How to delete documents from collection in MongoDB?

Deleting documents from collection has two methods: a)deleteOne(filter) b) deleteMany(filter). Both methods accept filter and options optionally where the filter is used to filter the documents for finding.

With deleteOne() we can update a single document of a collection. If we pass a filter for updateOne() it will delete first matched document.

db.collectionName.deleteOne(filter)

With deleteMany() we can delete all the documents from the collection. If we pass the filter to deleteMany(), it will delete all the documents that matched the filter.

db.collectionName.deleteMany(filter)

The remove method removes (i.e. deletes) documents from a collection. You can delete all documents, some documents, or a single document as required. Here’s an example of deleting a specific document.

db.employees.remove({“_id”:3})