Summary: in this tutorial, you’ll learn how to use the MongoDB deleteMany()
method to delete all documents that match a condition from a collection.
Introduction to MongoDB deleteMany() method
The deleteMany()
method allows you to remove all documents that match a condition from a collection.
The deleteMany()
has the following syntax:
db.collection.deleteMany(filter, option)
Code language: CSS (css)
In this syntax:
filter
is a document that specifies deletion criteria. If the filter is an empty document{}
, thedeleteMany()
method will delete all documents from the collection.option
is a document that specifies the deletion option.
The deleteMany()
returns a document containing the deleteCount
field that stores the number of deleted documents.
To delete a single document from a collection, you can use the deleteOne()
method.
MongoDB deleteMany()
method examples
We’ll use the following products
collection.
db.products.insertMany([
{ "_id" : 1, "name" : "xPhone", "price" : 799, "releaseDate": ISODate("2011-05-14"), "spec" : { "ram" : 4, "screen" : 6.5, "cpu" : 2.66 },"color":["white","black"],"storage":[64,128,256]},
{ "_id" : 2, "name" : "xTablet", "price" : 899, "releaseDate": ISODate("2011-09-01") , "spec" : { "ram" : 16, "screen" : 9.5, "cpu" : 3.66 },"color":["white","black","purple"],"storage":[128,256,512]},
{ "_id" : 3, "name" : "SmartTablet", "price" : 899, "releaseDate": ISODate("2015-01-14"), "spec" : { "ram" : 12, "screen" : 9.7, "cpu" : 3.66 },"color":["blue"],"storage":[16,64,128]},
{ "_id" : 4, "name" : "SmartPad", "price" : 699, "releaseDate": ISODate("2020-05-14"),"spec" : { "ram" : 8, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256,1024]},
{ "_id" : 5, "name" : "SmartPhone", "price" : 599,"releaseDate": ISODate("2022-09-14"), "spec" : { "ram" : 4, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256]}
])
Code language: JavaScript (javascript)
1) Using the deleteMany() method to delete multiple documents
The following example uses the deleteMany()
method to delete all documents where the value in the price
field is 899
:
db.products.deleteMany({ price: 899 })
Code language: CSS (css)
In this example, the filter
argument is { price: 899 }
that matches documents whose price is 899
.
It returned the following document:
{ "acknowledged" : true, "deletedCount" : 2 }
Code language: JSON / JSON with Comments (json)
The deleteCount
field indicates that the number of deleted documents is 2.
2) Using the deleteMany() method to delete all documents
The following query uses the deleteMany()
method to delete all documents from the products
collection:
db.products.deleteMany({})
Code language: CSS (css)
Output:
{ acknowledged: true, deletedCount: 3 }
Code language: CSS (css)
In this example, we passed an empty document {}
into the deleteMany()
method. Therefore, it deleted all documents from the products
collection.
Summary
- Use the
deleteMany()
method to remove all documents that match a condition from a collection. - Pass an empty document
{}
into thedeleteMany()
method to remove all documents from the collection.