Delete Documents in MongoDB

 

Delete Documents

The MongoDB shell provides the following methods to delete documents from a collection:

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_mflix
db.movies.deleteMany({})

The method returns a document with the status of the operation. For more information and examples, see deleteMany().

 

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":

use sample_mflix
db.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_mflix
db.movies.deleteOne( { cast: "Brad Pitt" } )


Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form