Query.prototype.update()

Parameters
  • [filter] «Object»
  • [doc] «Object» the update command

  • [options] «Object»

  • [options.multipleCastError] «Boolean» by default, mongoose only returns the first error that occurred in casting the query. Turn on this option to aggregate all the cast errors.

  • [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.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.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.

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

Returns:
  • «Query» this

Declare and/or execute this query as an update() operation.

All paths passed that are not atomic operations will become $set ops.

This function triggers the following middleware.

  • update()

Example

  1. Model.where({ _id: id }).update({ title: 'words' })
  2. // becomes
  3. Model.where({ _id: id }).update({ $set: { title: 'words' }})

Valid options:

  • 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)
  • context (string) if set to ‘query’ and runValidators is on, this will refer to the query in custom validator functions that update validation runs. Does nothing if runValidators is false.
  • read
  • writeConcern

Note

Passing an empty object {} as the doc will result in a no-op unless the overwrite option is passed. Without the overwrite option set, the update operation will be ignored and the callback executed without sending the command to MongoDB so as to prevent accidently overwritting documents in the collection.

Note

The operation is only executed when a callback is passed. To force execution without a callback, we must first call update() and then execute it by using the exec() method.

  1. const q = Model.where({ _id: id });
  2. q.update({ $set: { name: 'bob' }}).update(); // not executed
  3. q.update({ $set: { name: 'bob' }}).exec(); // executed
  4. // keys that are not [atomic](https://docs.mongodb.com/manual/tutorial/model-data-for-atomic-operations/#pattern) ops become `$set`.
  5. // this executes the same command as the previous example.
  6. q.update({ name: 'bob' }).exec();
  7. // overwriting with empty docs
  8. const q = Model.where({ _id: id }).setOptions({ overwrite: true })
  9. q.update({ }, callback); // executes
  10. // multi update with overwrite to empty doc
  11. const q = Model.where({ _id: id });
  12. q.setOptions({ multi: true, overwrite: true })
  13. q.update({ });
  14. q.update(callback); // executed
  15. // multi updates
  16. Model.where()
  17. .update({ name: /^match/ }, { $set: { arr: [] }}, { multi: true }, callback)
  18. // more multi updates
  19. Model.where()
  20. .setOptions({ multi: true })
  21. .update({ $set: { arr: [] }}, callback)
  22. // single update by default
  23. Model.where({ email: 'address@example.com' })
  24. .update({ $inc: { counter: 1 }}, callback)

API summary

  1. update(filter, doc, options, cb) // executes
  2. update(filter, doc, options)
  3. update(filter, doc, cb) // executes
  4. update(filter, doc)
  5. update(doc, cb) // executes
  6. update(doc)
  7. update(cb) // executes
  8. update(true) // executes
  9. update()