What are all the possible callback options for updating Mongoose Document #?

This seems very poorly documented. The documentation example contains only a callback for updating. There is a link redirected to Model.update here , and the example shows callback parameters (err, numberAffected, raw) .

Document update callback # same parameters? I was hoping to return an updated document. My initial search was based on updating a document in mongo db , but even the answer there does not explain or even list the callback parameters.

+6
source share
2 answers

Poor documentation of callback parameters is something that, for some reason, has plagued many node.js libraries. But the MongoDB update command (regardless of the driver) does not provide access to the updated document, so you can be sure that it was not included in the callback.

If you want to update the document, you can use one of the findAndModify methods, for example findOneAndUpdate :

 MyModel.findOneAndUpdate({_id: 1}, {$inc: {count: 1}}, {new: true}, function (err, doc) { // doc contains the modified document }); 

Starting with Mongoose 4.0, you need to provide the {new: true} option in the call to get the updated document, since the default is false , which returns the original.

+9
source

num-affected is really the original output of mongoDB and Object . It looks like this:

 {ok: 1, nModified: 0, n: 1} 

Unfortunately, I do not know what nModified means. 'n' is the old (up to 4.0) number of rows affected

0
source

All Articles