What Are The Delete Methods Available In MongoDB?
1 min readDec 5, 2023
The following techniques are available in MongoDB for removing documents from a collection:
db.collection.deleteOne():
Even though numerous documents may meet the specified filter, only one document that matches the filter should be deleted.
db.collection.deleteMany():
Eliminate every document that fits a given filter.
db.collection.remove():
Eliminate a specific document or every document that fits the given filter.
Starting in MongoDB 6.1:
- MongoDB may batch delete several documents at once in order to increase efficiency.
- There is a new BATCHED_DELETE stage for batch document deletions in the explain command results.
Additional Methods
Documents from a collection can also be removed using the following techniques:
db.collection.findOneAndDelete():
The sort option is provided by findOneAndDelete(). The first document sorted in the designated order may be deleted with this option.db.collection.findAndModify()
.db.collection.findAndModify()
offers a sorting option. The first document sorted in the designated order may be deleted with this option.db.collection.bulkWrite()
.
For further details and examples, look to the methods’ respective reference pages.