Query.prototype.findOneAndReplace()
Parameters
- [filter] «Object»
- [replacement] «Object»
[options] «Object»
[options.rawResult] «Boolean» if true, returns the raw result from the MongoDB driver
[options.session=null] «ClientSession» The session associated with this query. See transactions docs.
[options.strict] «Boolean|String» overwrites the schema’s strict mode option
[options.new=false] «Boolean» By default,
findOneAndUpdate()
returns the document as it was beforeupdate
was applied. If you setnew: true
,findOneAndUpdate()
will instead give you the object afterupdate
was applied.[options.lean] «Object» if truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. See
Query.lean()
and the Mongoose lean tutorial.[options.session=null] «ClientSession» The session associated with this query. See transactions docs.
[options.strict] «Boolean|String» overwrites the schema’s strict mode option
[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.timestamps=null] «Boolean» If set to
false
and schema-level timestamps are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.[options.returnOriginal=null] «Boolean» An alias for the
new
option.returnOriginal: false
is equivalent tonew: true
.[callback] «Function» optional params are (error, document)
Returns:
- «Query» this
Issues a MongoDB findOneAndReplace command.
Finds a matching document, removes it, and passes the found document (if any) to the callback. Executes if callback
is passed.
This function triggers the following middleware.
findOneAndReplace()
Available options
sort
: if multiple docs are found by the conditions, sets the sort order to choose which doc to updatemaxTimeMS
: puts a time limit on the query - requires mongodb >= 2.6.0rawResult
: if true, resolves to the raw result from the MongoDB driver
Callback Signature
function(error, doc) {
// error: any errors that occurred
// doc: the document before updates are applied if `new: false`, or after updates if `new = true`
}
Examples
A.where().findOneAndReplace(filter, replacement, options, callback); // executes
A.where().findOneAndReplace(filter, replacement, options); // return Query
A.where().findOneAndReplace(filter, replacement, callback); // executes
A.where().findOneAndReplace(filter); // returns Query
A.where().findOneAndReplace(callback); // executes
A.where().findOneAndReplace(); // returns Query