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 8860801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:20:31+00:00 2026-06-14T15:20:31+00:00

I have embedded comments in a posts model. I am using mongoosejs. After pushing

  • 0

I have embedded comments in a posts model. I am using mongoosejs. After pushing a new comment in a post, I want to access the id of the newly added embedded comment. Not sure how to get it.

Here is how the code looks like.

var post = Post.findById(postId,function(err,post){

   if(err){console.log(err);self.res.send(500,err)}

   post.comments.push(comment);

   post.save(function(err,story){
       if(err){console.log(err);self.res.send(500,err)}
           self.res.send(comment);
   })


});

In the above code, the id of the comment is not returned. Note there is a _id field which is created in the db.

The schema looks like

var CommentSchema = new Schema({
  ...
})

var PostSchema = new Schema({
    ...
    comments:[CommentSchema],
    ...
});
  • 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-06-14T15:20:33+00:00Added an answer on June 14, 2026 at 3:20 pm

    A document’s _id value is actually assigned by the client, not the server. So the _id of the new comment is available right after you call:

    post.comments.push(comment);
    

    The embedded doc pushed to post.comments will have its _id assigned as it’s added, so you can pull it from there:

    console.log('_id assigned is: %s', post.comments[post.comments.length-1]._id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Item class and a Comment class. Items have many embedded comments.
assume that i have a BlogPost model with zero-to-many embedded Comment documents. can i
I have embedded a Google Map in my website. I want to change its
I have embedded a JTextArea on a JScrollPane and am using that JTextArea for
I have embedded an IronPython engine in a C# application. I want to expose
I have the following problem. I have embedded a map using Google Maps' API.
I have several embedded linux systems that I want to write a 'Who's Online?'
Here is a simple scenario: I have a blog post with comments attach to
Lets say we have a posts collection, and embedded in posts is an array
I have a document structure in MongoDB like this: User |---> Posts |----> Comments

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.