collection.findOneAndDelete()¶
Definition¶
collection.findOneAndDelete()
¶
Remove a single document from a collection based on a query filter and
return a document with the same form as the document immediately before
it was deleted. Unlike collection.deleteOne()
, this action
allows you to atomically find and delete a document with the same
command. This avoids the risk of other update operations changing the
document between separate find
and delete operations.
Usage¶
Example¶
To call the collection.findOneAndDelete()
action from a
Function, get a collection handle with
database.collection()
then call the handle's
findOneAndUpdate()
method.
// Find the first document that has a quantity greater than 25 const query = { "quantity": { "$gte": 25 } }; // Sort the documents in order of descending quantity before // deleting the first one. const options = { "sort": { "quantity": -1 } } return itemsCollection.findOneAndDelete(query, options) .then(deletedDocument => { if(deletedDocument) { console.log(`Successfully deleted document that had the form: ${deletedDocument}.`) } else { console.log("No document matches the provided query.") } return deletedDocument }) .catch(err => console.error(`Failed to find and delete document: ${err}`))
Parameters¶
The collection.findOneAndDelete()
action has the following
form:
findOneAndDelete(query, options)
Parameter | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Query Filter query: <document> | Required. A standard MongoDB query document that specifies which document to delete. You can use most query selectors except for evaluation, geospatial, or bitwise selectors. If multiple documents match the query, only the first document in sort order or natural order will be updated. | ||||||||||
Delete Options options: <document> | A document that specifies configuration options for the query.
The
| ||||||||||
Sort options.sort: <document> | Optional. Specifies the query sort order. Sort documents
specify one or more fields to sort on where the value of each
field indicates whether MongoDB should sort it in ascending
( Example The following sort document specifies that documents should be
sorted first by
| ||||||||||
Projection options.projection: <document> | Optional. A document that specifies which fields MongoDB should return or withhold in each document that matches the query. To return all fields in the matching documents, omit this
parameter or specify an empty projection document ( To return specific fields and the document's
To withhold specific fields, specify the fields in the projection
document with a value of
Note You may specify either fields to include or fields to withhold
but not both. For example, the following projection is
invalid because it simultaneously includes the
The exception to this rule is the
|
Return Value¶
The collection.findOneAndDelete()
action returns a Promise that resolves to a
single document that the query deleted. If no documents match the
specified query, the promise resolves to null
.
Promise<document|null>