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()
.
The examples on this page reference the Atlas sample dataset. You can create a free Atlas cluster and populate that cluster with sample data to follow along with these examples. To learn more, see Get Started with Atlas.
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:
use sample_mflixdb.movies.deleteMany({})
The method returns a document with the status of the operation. For more information and examples, see deleteMany()
.
If you want to delete all documents from a large collection, dropping with the db.collection.drop()
method. and recreating the collection may have faster performance than deleting documents with the db.collection.deleteMany()
method. When you recreate the collection, you must also recreate any specified collection parameters such as collection indexes.
You can specify criteria, or filters, that identify the documents to delete. The filters use the same syntax as read operations.
To specify equality conditions, use <field>:<value>
expressions in the query filter document.
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"
:
use sample_mflixdb.movies.deleteMany( { title: "Titanic" } )
The method returns a document with the status of the operation. For more information and examples, see deleteMany()
.
To delete at most a single document that matches a specified filter (even though multiple documents may match the specified filter) use the db.collection.deleteOne()
method.
To delete the first document from the sample_mflix.movies
collection where the cast
array contains "Brad Pitt"
:
use sample_mflixdb.movies.deleteOne( { cast: "Brad Pitt" } )
Note
MongoDB preserves a natural sort order for documents. This ordering is an internal implementation feature, and you should not rely on any particular structure within it. To learn more, see natural order.
To learn more about the specific behavior of deleting documents, see Behavior.
To see additional examples of deleting documents, see the following method pages:
To see all available methods to delete documents, see Delete Methods.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4