MongoDB Delete Document
MongoDB Delete Document – In this MongoDB Tutorial, we shall learn to delete MongoDB Document(s).
To delete documents from MongoDB Collection, follow this step by step guide :
- Prepare a Criteria to select those documents for deletion.
criteria = {field1:value1, field2:value2,..} - [Optional] Query the Documents based on the Criteria formed to cross verify if you want to delete all those selected Documents.
db.collection.find(criteria) - Use db.collection.remove() method to remove the records based on criteria.
db.collection.remove(criteria)
Syntax of remove() method
Following is the syntax of remove() method to delete documents from a collection in MongoDB :
db.collection_name.remove(CRITERIA, JUST_ONE)
where
collection_name | String | [mandatory] Name of the Collection from which you would like to remove Documents |
CRITERIA | Document | [optional] The criteria that selects the required documents to delete |
JUST_ONE | 1 or true | [optional] If one or true, deletes only one document from the selection |
Example to Delete Documents based on Criteria
In this example we shall delete only some of the entries of a selection using a criteria. Following is the collection we consider in this example
> db.customers.find({})
{ "_id" : ObjectId("59edda4b5f82df4555f2bfa6"), "name" : "Midhuna", "age" : 23, "place" : "Amaravati" }
{ "_id" : ObjectId("59edda4b5f82df4555f2bfa7"), "name" : "Akhil", "age" : 24, "place" : "New York" }
{ "_id" : ObjectId("59edda4b5f82df4555f2bfa8"), "name" : "Honey", "age" : 25 }
{ "_id" : ObjectId("59edda4b5f82df4555f2bfa9"), "name" : "Manju", "age" : 23, "place" : "Amaravati" }
{ "_id" : ObjectId("59edda4b5f82df4555f2bfaa"), "name" : "Bharat", "age" : 24, "place" : "New York" }
{ "_id" : ObjectId("59edda4b5f82df4555f2bfab"), "name" : "Arya", "age" : 25 }
Now we shall form a criteria,
> criteria = {"age" : 23}
{ "age" : 23 }
Check the entries that could be selected using the criteria
> db.customers.find(criteria)
{ "_id" : ObjectId("59edda4b5f82df4555f2bfa6"), "name" : "Midhuna", "age" : 23, "place" : "Amaravati" }
{ "_id" : ObjectId("59edda4b5f82df4555f2bfa9"), "name" : "Manju", "age" : 23, "place" : "Amaravati" }
There are two entries for this criteria. Once we are sure that we can delete these documents, delete them using remove() method as shown below :
> db.customers.remove(criteria)
WriteResult({ "nRemoved" : 2 })
MongoDB acknowledged with result of the Operation, { “nRemoved” : 2 } , saying two documents are deleted.
Example to Delete All Documents present in the Collection
In this example, we shall delete all documents present in a collection named customers .
> db.customers.count()
4
There are four documents in the collection.
To delete all documents in the collection, run db.collection.remove() method with an empty query.
> db.customers.remove({})
WriteResult({ "nRemoved" : 4 })
All the documents were deleted from the collection.
> db.customers.count()
0
Example to delete only one Document despite many Documents selected for the criteria
For the following criteria there are two documents.
> criteria={age:25}
{ "age" : 25 }
> db.customers.find(criteria).count()
2
Now we shall delete only one document using justOne argument of remove() method
> db.customers.remove(criteria,1)
WriteResult({ "nRemoved" : 1 })
Despite multiple Documents for the criteria, only one document is deleted because of the justOne argument. And the rest of the documents continue to exist in the collection.
> db.customers.find(criteria).count()
1
Conclusion :
In this MongoDB Tutorial – MongoDB Delete Document(s), we have learnt to delete one or more documents based on a criteria and also to delete all the documents with examples.