Parameters
- [filter] «Object|Query»
- [doc] «Object»
[options.rawResult] «Boolean» if true, returns the
[options.strict] «Boolean|String» overwrites the schema’s strict mode option
[options.session=null] «ClientSession» The session associated with this query. See .
[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.lean] «Object» if truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. See
Query.lean()
and .[options.session=null] «ClientSession» The session associated with this query. See transactions docs.
[options.strict] «Boolean|String» overwrites the schema’s
[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 deletebaz
from the update inModel.updateOne({}, { foo: 'bar', baz: undefined })
before sending the update to the server.[options.returnOriginal=null] «Boolean» An alias for the
new
option.returnOriginal: false
is equivalent tonew: true
.[callback] «Function» optional params are (error, doc), unless is used, in which case params are (error, writeOpResult)
Returns:
- «Query» this
Issues a mongodb update command.
Finds a matching document, updates it according to the update
arg, passing any options
, and returns the found document (if any) to the callback. The query executes if callback
is passed.
findOneAndUpdate()
Available options
new
: bool - if true, return the modified document rather than the original. defaults to false (changed in 4.0)upsert
: bool - creates the object if it doesn’t exist. defaults to false.fields
: {Object|String} - Field selection. Equivalent to.select(fields).findOneAndUpdate()
maxTimeMS
: puts a time limit on the query - requires mongodb >= 2.6.0runValidators
: if true, runs on this command. Update validators validate the update operation against the model’s schema.setDefaultsOnInsert
: if this and 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 .rawResult
: if true, returns the raw result from the MongoDB drivercontext
(string) if set to ‘query’ andrunValidators
is on,this
will refer to the query in custom validator functions that update validation runs. Does nothing ifrunValidators
is false.
Callback Signature
Examples
query.findOneAndUpdate(conditions, update, options, callback) // executes
query.findOneAndUpdate(conditions, update, options) // returns Query
query.findOneAndUpdate(conditions, update, callback) // executes
query.findOneAndUpdate(conditions, update) // returns Query
query.findOneAndUpdate(update, callback) // returns Query
query.findOneAndUpdate(update) // returns Query