Model.update()

Parameters
  • filter «Object»
  • doc «Object»
  • [options] «Object» optional see Query.prototype.setOptions()

  • [options.strict] «Boolean|String» overwrites the schema’s strict mode option

  • [options.upsert=false] «Boolean» if true, and no documents found, insert a new document

  • [options.writeConcern=null] «Object» sets the write concern for replica sets. Overrides the schema-level write concern

  • [options.omitUndefined=false] «Boolean» If true, delete any properties whose value is undefined when casting an update. In other words, if this is set, Mongoose will delete baz from the update in Model.updateOne({}, { foo: 'bar', baz: undefined }) before sending the update to the server.

  • [options.multi=false] «Boolean» whether multiple documents should be updated or just the first one that matches filter.

  • [options.runValidators=false] «Boolean» if true, runs update validators on this command. Update validators validate the update operation against the model’s schema.

  • [options.setDefaultsOnInsert=false] «Boolean» 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.

  • [options.timestamps=null] «Boolean» If set to false and schema-level timestamps are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.

  • [options.overwrite=false] «Boolean» By default, if you don’t include any update operators in doc, Mongoose will wrap doc in $set for you. This prevents you from accidentally overwriting the document. This option tells Mongoose to skip adding $set.

  • [callback] «Function» params are (error, updateWriteOpResult)

  • [callback] «Function»

Returns:
  • «Query»

Updates one document in the database without returning it.

This function triggers the following middleware.

  • update()

Examples:

  1. MyModel.update({ age: { $gt: 18 } }, { oldEnough: true }, fn);
  2. const res = await MyModel.update({ name: 'Tobi' }, { ferret: true });
  3. res.n; // Number of documents that matched `{ name: 'Tobi' }`
  4. // Number of documents that were changed. If every doc matched already
  5. // had `ferret` set to `true`, `nModified` will be 0.
  6. res.nModified;

Valid options:

  • strict (boolean): overrides the schema-level strict option for this update
  • upsert (boolean): whether to create the doc if it doesn’t match (false)
  • writeConcern (object): sets the write concern for replica sets. Overrides the schema-level write concern
  • omitUndefined (boolean): If true, delete any properties whose value is undefined when casting an update. In other words, if this is set, Mongoose will delete baz from the update in Model.updateOne({}, { foo: 'bar', baz: undefined }) before sending the update to the server.
  • 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 (boolean): 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.
  • timestamps (boolean): If set to false and schema-level timestamps are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.
  • overwrite (boolean): disables update-only mode, allowing you to overwrite the doc (false)

All update values are cast to their appropriate SchemaTypes before being sent.

The callback function receives (err, rawResponse).

  • err is the error if any occurred
  • rawResponse is the full response from Mongo

Note:

All top level keys which are not atomic operation names are treated as set operations:

Example:

  1. const query = { name: 'borne' };
  2. Model.update(query, { name: 'jason bourne' }, options, callback);
  3. // is sent as
  4. Model.update(query, { $set: { name: 'jason bourne' }}, options, function(err, res));
  5. // if overwrite option is false. If overwrite is true, sent without the $set wrapper.

This helps prevent accidentally overwriting all documents in your collection with { name: 'jason bourne' }.

Note:

Be careful to not use an existing model instance for the update clause (this won’t work and can cause weird behavior like infinite loops). Also, ensure that the update clause does not have an _id property, which causes Mongo to return a “Mod on _id not allowed” error.

Note:

Mongoose casts values and runs setters when using update. The following features are not applied by default.

If you need document middleware and fully-featured validation, load the document first and then use save().

  1. Model.findOne({ name: 'borne' }, function (err, doc) {
  2. if (err) ..
  3. doc.name = 'jason bourne';
  4. doc.save(callback);
  5. })