In this chapter, we will talk about MongoDB delete documents – how to delete documents in the collection. There are many ways in MongoDB from which we can delete documents in the collection:
- collection.remove()
- collection.deleteOne() – New in version 3.2.
- collection.deleteMany() – New in version 3.2.
- collection.findOneAndDelete()
Let’s read about them in brief.
MongoDB Delete Documents
There are many methods to delete documents in MongoDB as mentioned above. Let’s go through these methods to understand it better.
-
db.collection.remove()
This method deletes a single document or all documents from the collection that match a specified filter. db.collection.remove() method (remove method) works on two parameters.
- Deletion criteria: Condition on which document has to be deleted. (Match a deletion criteria)
- JustOne: To remove only one document when set to true or 1.
Syntax:
db.collection_name.remove (DELETION_CRITERIA, JustOne) //where JustOne is optional
E.g.: To remove all documents that match a condition:
db.TutorialsJar.remove({tutorial : "mongodb" } )
This command will delete all the documents from the ‘TutorialsJar’ collection where ‘tutorial’ field is equal to ‘mongodb’.
E.g.: To remove a single document that matches a condition:
If you want to remove a single document that matches a given condition, then use the following syntax.
db.TutorialsJar.remove({tutorial : "mongodb" }, 1 )
E.g.: To delete all the documents from the collection (i.e. pass an empty filter without any condition)
db.TutorialsJar.remove({})
2) db.collection.deleteOne()
This method deletes at most a single document that matches a specified filter even though multiple documents may match the specified filter. This is a new MongoDB delete documents method introduces in v3.2.
Syntax:
db.TutorialsJar.deleteOne({tutorial : "mongodb" } )
This is similar to db.collection.remove() method with the <justOne> parameter set to true or 1.
3) db.collection.deleteMany()
This method deletes all documents that match a specified filter. This is also a new MongoDB delete documents method introduces in v3.2.
db.TutorialsJar.deleteMany({tutorial : "mongodb" } )
This command will delete all the documents from the ‘TutorialsJar’ collection where ‘tutorial’ field is equal to ‘mongodb’.
4) db.collection.findOneAndDelete()
This method deletes a single document based on the filter and sort criteria, returning the deleted document.
Definition:
db.collection.findOneAndDelete(filter, options)
Syntax:
db.collection.findOneAndDelete( <filter>, { projection: <document>, sort: <document>, maxTimeMS: <number>, } )
That’s it in this MongoDB delete documents – MongoDB CRUD Operations Part 3. If you have any queries, please comment in the comment section below. Also, do share your feedback 🙂
- MongoDB Operators Tutorial – What are Different Operators Available? - October 5, 2019
- MongoDB Projection Tutorial : Return Specific Fields From Query - March 9, 2019
- MongoDB Index Tutorial – Create Index & MongoDB Index Types - July 6, 2018