How to return the updated document on Mongoose

If you are working with Mongoose, maybe noticed that the update’s callback is not returning the post-update object. Didn’t you noticed it? Let’s see how to return the updated document on Mongoose.

Return the updated document on Mongoose

By default, Mongoose don’t return the post-update document. But, there is an option to force it.

Using any find & update function like findByIdAndUpdate(), findAndUpdate() or findOneAndUpdate() just add the third param with new: true.

Example

var query   = { id: 8 }; 
var update  = { title: "new title" }; 
var options = { new: true }; 
MyModel.findOneAndUpdate(query, update, options, function(err, doc){ 
  // Done! 
  // doc.title = "new title" 
});

Note the third parameter with new: true, it produces that doc on callback function it’s the post-update document.

Feel free to leave your doubt, problem or experience 😀