Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7174441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:03:13+00:00 2026-05-28T16:03:13+00:00

In the Mongoose documentation at the following address: http://mongoosejs.com/docs/embedded-documents.html There is a statement: DocumentArrays

  • 0

In the Mongoose documentation at the following address:
http://mongoosejs.com/docs/embedded-documents.html

There is a statement:

DocumentArrays have an special method id that filters your embedded
documents by their _id property (each embedded document gets one):

Consider the following snippet:

 post.comments.id(my_id).remove();
  post.save(function (err) {
    // embedded comment with id `my_id` removed!
  });

I’ve looked at the data and there are no _ids for the embedded documents as would appear to be confirmed by this post:

How to return the last push() embedded document

My question is:

Is the documentation correct? If so then how do I find out what ‘my_id’ is (in the example) to do a ‘.id(my_id)’ in the first place?

If the documentation is incorrect is it safe to use the index as an id within the document array or should I generate a unique Id manually (as per the mentioned post).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:03:14+00:00Added an answer on May 28, 2026 at 4:03 pm

    Instead of doing push() with a json object like this (the way the mongoose docs suggest):

    // create a comment
    post.comments.push({ title: 'My comment' });
    

    You should create an actual instance of your embedded object and push() that instead. Then you can grab the _id field from it directly, because mongoose sets it when the object is instantiated. Here’s a full example:

    var mongoose = require('mongoose')
    var Schema = mongoose.Schema
    var ObjectId = Schema.ObjectId
    
    mongoose.connect('mongodb://localhost/testjs');
    
    var Comment = new Schema({
        title     : String
      , body      : String
      , date      : Date
    });
    
    var BlogPost = new Schema({
        author    : ObjectId
      , title     : String
      , body      : String
      , date      : Date
      , comments  : [Comment]
      , meta      : {
            votes : Number
          , favs  : Number
        }
    });
    
    mongoose.model('Comment', Comment);
    mongoose.model('BlogPost', BlogPost);
    
    var BlogPost = mongoose.model('BlogPost');
    var CommentModel = mongoose.model('Comment')
    
    
    var post = new BlogPost();
    
    // create a comment
    var mycomment = new CommentModel();
    mycomment.title = "blah"
    console.log(mycomment._id) // <<<< This is what you're looking for
    
    post.comments.push(mycomment);
    
    post.save(function (err) {
      if (!err) console.log('Success!');
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use mongoose in my node.js app, and basically have the following models: //
I'm using Mongoose with Node.js and have the following code that will call the
I am using Mongoose + CoffeeScript and when I try to add embedded documents
Does anyone know how to test Mongoose Validations? Example, I have the following Schema
http://code.google.com/p/mongoose/ http://shttpd.sourceforge.net/shttpd.1.txt Trying to configure the lightweight web server shttpd now called mongoose to
i've got Node.js server and with Exressjs/Mongoose im trying to insert some embedded documents
I have a mongoose model in my node.js application, representing invoices. I have figured
I am trying to figure out how to use mongoose-joins for Node.js. I have
I have a set of nested queries with express/mongoose, pretty much like so: app.get(...,
I've got a base url. http://baseurl.com/ I'm trying to run projects on the back

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.