Delete Documents
The MongoDB shell provides the following methods to delete documents from a collection:
To delete multiple documents, use
db.collection.deleteMany()
.To delete a single document, use
db.collection.deleteOne()
.
Delete All Documents
To delete all documents from a collection, pass an empty filter document {}
to the db.collection.deleteMany()
method.
To delete all documents from the sample_mflix.movies
collection:
The method returns a document with the status of the operation. For more information and examples, see deleteMany()
.
Delete All Documents that Match a Condition
You can specify criteria, or filters, that identify the documents to delete. The filters use the same syntax as read operations.
To delete all documents that match a deletion criteria, pass a filter parameter to the deleteMany()
method.
To delete all documents from the sample_mflix.movies
collection where the title
equals "Titanic"
:
The method returns a document with the status of the operation. For more information and examples, see deleteMany()
.
Delete Only One Document that Matches a Condition
db.collection.deleteOne()
method.To delete the first document from the sample_mflix.movies
collection where the cast
array contains "Brad Pitt"
: