- MongoDB CRUD Operations >
- Delete Documents
Delete Documents¶
On this page
Delete Methods¶
MongoDB provides the following methods to delete documents of a collection:
db.collection.remove() |
Delete a single document or all documents that match a specified filter. |
db.collection.deleteOne() |
Delete at most a single document that match a specified filter even though multiple documents may match the specified filter. New in version 3.2. |
db.collection.deleteMany() |
Delete all documents that match a specified filter. New in version 3.2. |
You can specify criteria, or filters, that identify the documents to delete. These filters use the same syntax as read operations:
A query filter document can specify equality condition with
<field>:<value>
expressions to select all documents that contain the<field>
with the specified<value>
:A query filter document can use the query operators to specify conditions in the following form:
Delete Behavior¶
Indexes¶
Delete operations do not drop indexes, even if deleting all documents from a collection.
Atomicity¶
All write operations in MongoDB are atomic on the level of a single document. For more information on MongoDB and atomicity, see Atomicity and Transactions.
Example Collection¶
This page provides examples of remove operations in the
mongo
shell. To populate the users
collection referenced
in the examples, run the following in mongo
shell:
Note
If the users
collection already contains documents with the same
_id
values, you need to drop
the collection (db.users.drop()
) before inserting the example
documents.
Delete All Documents¶
To remove all documents from a collection, pass an empty filter document {}
to either the
db.collection.deleteMany()
or the
db.collection.remove()
method.
db.collection.deleteMany()
¶
The following example uses the db.collection.deleteMany()
method to delete all documents from the users
collection:
The method returns a document with the status of the operation:
For more information and examples, see db.collection.deleteMany()
.
db.collection.remove()
¶
Alternatively, the following example uses the
db.collection.remove()
method to delete all documents from
the users
collection:
To delete all documents from a collection, it may be more efficient to
use the db.collection.drop()
method to drop the entire
collection, including the indexes, and then recreate the collection and
rebuild the indexes.
Delete All Documents that Match a Condition¶
To delete all documents that match a deletion criteria, pass a
filter parameter to either
db.collection.deleteMany()
method or the
db.collection.remove()
method.
db.collection.deleteMany()
¶
The following example uses db.collection.deleteMany()
to
remove all documents from the users
collection where the status
field equals "A"
:
The method returns a document with the status of the operation:
db.collection.remove()
¶
Alternatively, the following example uses
db.collection.remove()
to remove all documents from the
users
collection where the status
field equals "P"
:
For large deletion operations, it may be more efficient to copy the
documents that you want to keep to a new collection and then use
db.collection.drop()
on the original collection.
Remove Only One Document that Matches a Condition¶
To delete at most a single document that match a specified filter,even
though multiple documents may match the specified filter, use either
the db.collection.deleteOne()
method or the
db.collection.remove()
method with the <justOne>
parameter set to true
or 1
.
db.collection.deleteOne()
¶
The following example uses db.collection.deleteOne()
to
delete the first document where status
is "D"
.
db.collection.remove()
¶
Alternatively, the following example uses the
db.collection.remove()
with the <justOne>
parameter set
to 1
to delete the first document where status
is
"D"
:
See also
Additional Methods¶
The following methods can also delete documents from a collection:
db.collection.findOneAndDelete()
.findOneAndDelete() provides a sort option. The option allows for the deletion of the first document sorted by the specified order.
db.collection.findOneAndModify()
.db.collection.findOneAndModify()
provides a sort option. The option allows for the deletion of the first document sorted by the specified order.
Write Acknowledgement¶
With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.