I'm looking at the example here for Document#update in Mongoose:
http://mongoosejs.com/docs/api.html#document_Document-update
Method signature:
Document#update(doc, options, callback)
Example code:
weirdCar.update({$inc: {wheels:1}}, { w: 1 }, callback);
The documentation says the first parameter should be "doc" but what exactly is doc? I would imagine doc should just be an object that maps keys to new values to be updated (by default $set is used). In the code example they are trying to increment wheels by 1.
Then in the example they pass {w : 1} as options but "w" is not a valid option according to Model.Update. The only valid options should be: safe, upsert, multi, strict.
http://mongoosejs.com/docs/api.html#model_Model.update
Can someone explain the example code provided by Mongoose?
The key for me was the correct parameters. You need to supply the callback
parameter or call .exec()
on the result for it to work.
var Product = mongoose.model('product', mongoose.Schema({
name: String
}));
Product.findById('539dceccc61fa4950b43423a', function (err, product) {
product.update({ name: 'test' }, null, function(err, numberAffected, raw) { });
//or
product.update({ name: 'test' }).exec();
});
It seems to me like the docs are wrong.
Further, some people don't understand the issue here. We're trying to call update
on a DOCUMENT. NOT query for the document while performing an update. There's a difference, and it's not very well documented by Mongoose.
Here's what the documentation says: about document.update
example: weirdCar.update({$inc: {wheels:1}}, { w: 1 }, callback);
Parameters:
doc <Object>
options <Object>
callback <Function>
Valid Options
same as in Model.update
so lets have a look at Model.updates options...
Model.updates options:
There's nothing there that corresponds with that example... There is no mention of {w : 1}
or w
anything... So yes, it appears that the docs are wrong.
However, you can use it with or without those options.
So it looks like this without any options
weirdCar.update({$inc: {wheels:1}}, function(err, updated) {
// 'updated' is the object: {ok: number, nModified: number, n: number}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With