8

This seems really poorly documented.. The documentation example just has callback being passed to update. There is a link redirecting to Model.update here and the example shows the parameters of the callback are (err, numberAffected, raw).

Does the Document#update callback pass the same parameters? I was hoping to get the updated document returned. My initial search was based on how to update a document in mongo db but even the answer there doesn't explain or even list the params of the callback.

Community
  • 1
  • 1
JuJoDi
  • 14,627
  • 23
  • 80
  • 126

2 Answers2

14

Poor documentation of callback parameters is something that's plagued many node.js libraries for some reason. But MongoDB's update command (regardless of the driver) doesn't provide access to the updated doc, so you can be sure it's not provided to the callback.

If you want the updated document, then you can use one of the findAndModify methods like 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, as the default is now false which returns the original.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
0

num-affected is actually raw mongoDB output and an Object. It looks like this:

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

unfortunately I have no idea what nModified property means. 'n' is the old (pre 4.0) number of affected rows