Guest User

Untitled

a guest
Apr 20th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. User.findOneAndUpdate(
  2. {
  3. 'posts._id': req.params.id
  4. },
  5. {
  6. $push: {
  7. 'posts.$.comments': {
  8. from: {
  9. _id: req.user._id,
  10. username: req.user.username,
  11. },
  12. body: req.body.commentBody
  13. }
  14. }
  15. },
  16. err => {
  17. if (err) console.log(err)
  18. // Do something with ObjectId
  19. }
  20. );
  21.  
  22. var mypost = doc.posts.find(function(post){
  23. return post._id.toString()==req.params.id
  24. });
  25.  
  26. console.log(mypost.comments[mypost.comments.length]._id)
Add Comment
Please, Sign In to add comment