Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Document#update in Mongoose?

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?

like image 729
Kevin Wu Avatar asked Aug 21 '13 05:08

Kevin Wu


2 Answers

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();
});
like image 163
Kelly Selden Avatar answered Nov 01 '22 14:11

Kelly Selden


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:

  • safe (boolean) safe mode (defaults to value set in schema (true))
  • upsert (boolean) whether to create the doc if it doesn't match (false)
  • multi (boolean) whether multiple documents should be updated (false)
  • runValidators: if true, runs update validators on this command. Update validators validate the update operation against the model's schema.
  • setDefaultsOnInsert: if this and upsert are true, mongoose will apply the defaults specified in the model's schema if a new document is created. This option only works on MongoDB >= 2.4 because it relies on MongoDB's $setOnInsert operator.
  • strict (boolean) overrides the strict option for this update
  • overwrite (boolean) disables update-only mode, allowing you to overwrite the doc (false)

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}
})
like image 22
Augie Gardner Avatar answered Nov 01 '22 14:11

Augie Gardner