Model.findOneAndRemove()
Parameters
- conditions «Object»
[options] «Object» optional see
Query.prototype.setOptions()
[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.projection=null] «Object|String|Array<String>» optional fields to return, see
Query.prototype.select()
[callback] «Function»
Returns:
- «Query»
Issue a mongodb findAndModify remove command.
Finds a matching document, removes it, passing the found document (if any) to the callback.
Executes the query if callback
is passed.
This function triggers the following middleware.
findOneAndRemove()
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.0select
: sets the document fields to returnprojection
: like select, it determines which fields to return, ex.{ projection: { _id: 0 } }
rawResult
: if true, returns the raw result from the MongoDB driverstrict
: overwrites the schema’s strict mode option for this update
Examples:
A.findOneAndRemove(conditions, options, callback) // executes
A.findOneAndRemove(conditions, options) // return Query
A.findOneAndRemove(conditions, callback) // executes
A.findOneAndRemove(conditions) // returns Query
A.findOneAndRemove() // returns Query
Values are cast to their appropriate types when using the findAndModify helpers. However, the below are not executed by default.
- defaults. Use the
setDefaultsOnInsert
option to override.
findAndModify
helpers support limited validation. You can enable these by setting the runValidators
options, respectively.
If you need full-fledged validation, use the traditional approach of first retrieving the document.
Model.findById(id, function (err, doc) {
if (err) ..
doc.name = 'jason bourne';
doc.save(callback);
});