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?


The Mongoose api is based on the underlying MongoDB query structure. In this case, "doc" refers to a criteria for matching certain documents that you want to update. The MongoDB docs explain this very clearly: http://docs.mongodb.org/manual/core/write-operations/#update

So, let's say that you had a 'cars' collection, and you wanted to find all documents (all cars) that had 3 wheels, and increment that value so those cars had 4 wheels. In this case, "doc" is { wheels : 3 }, which returns all three-wheeled cars in the collection. Here is the basic query in the MongoDB shell:

> db.cars.update( { wheels : 3 }, { $inc : { wheels : 1 } } ); 

In Mongoose, you can add additional parameters for options and a callback function, but that's the basic idea.


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:

  • 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... {w : 1} is the error here. Ignore that part.

    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} })

    链接地址: http://www.djcxy.com/p/60686.html

    上一篇: Mongoose可以自动从Mongodb中提取模式吗?

    下一篇: 如何在Mongoose中使用Document#更新?