Query#
update([criteria], [doc], [options], [callback])
Declare
and/
or
execute this
query
as an
update() operation.
Parameters:
[criteria] <Object>
[doc] <Object> the
update command
[options] <Object>
[callback] <
Function>
Returns:
<
Query> this
See:
Model.
update
update
All paths passed that
are
not $atomic operations will become $
set ops.
Example
Model.
where({ _id: id }).
update({ title:
'words' })
// becomes
Model.
where({ _id: id }).
update({ $
set: { title:
'words' }})
Valid 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)
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.
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 (which would be an unsafe
write), we must
first
call
update()
and
then
execute it
by
using the
exec() method.
var q = Model.
where({ _id: id });
q.update({ $
set: { name: 'bob' }}).update(); // not executed
q.update({ $
set: { name: 'bob' }}).exec(); // executed as unsafe
// keys that are not $atomic ops become $
set.
// this executes the same command
as the previous example.
q.
update({ name:
'bob' }).
exec();
// overwriting with empty docs
var q = Model.where({ _id: id }).setOptions({ overwrite: true })
q.update({ }, callback); // executes
// multi
update with overwrite to empty doc
var q = Model.
where({ _id: id });
q.setOptions({ multi: true, overwrite: true })
q.update({ });
q.update(callback); // executed
// multi updates
Model.where()
.update({ name: /^match/ }, { $
set: { arr: [] }}, { multi: true }, callback)
// more multi updates
Model.
where()
.setOptions({ multi:
true })
.
update({ $
set: { arr: [] }}, callback)
// single
update
by
default
Model.
where({ email:
'address@example.com' })
.
update({ $inc: { counter:
1 }}, callback)
API summary
update(criteria, doc, options, cb) // executes
update(criteria, doc, options)
update(criteria, doc, cb) // executes
update(criteria, doc)
update(doc, cb) // executes
update(doc)
update(cb) // executes
update(
true) // executes (unsafe
write)
update()