If I have a document representing a post with comments like the one below:
{
"_id": "579a2a71f7b5455c28a7abcb",
"title": "post 1",
"link": "www.link1.com",
"__v": 0,
"comments": [
{
"author": "Andy",
"body": "Wish I had thought of that",
"_id": "579a2a71f7b5455c28a7abcd",
"upvotes": 0
},
{
"author": "Jim",
"body": "Just a comment",
"_id": "579a2a71f7b5455c28a7abcc",
"upvotes": 0
}
],
"upvotes": 5
}
When adding a new comment in the javascript code by pushing to the post.comments
array and saving the post using .save
with a callback, how can I retrieve the newly generated _id of the comment saved?
Even though the parent post document is available in the callback, it doesn't indicate which specific comment was inserted.
Are there any other document methods or alternate forms of the .save
callback that can address this situation?
Or should I resort to generating a unique id for the comment before saving, as I typically would?
EDITED: Using Mongoose, forgot to mention earlier!