Back to: MongoDB Tutorials
Deleting Document in MongoDB with Examples
In this article, I am going to discuss Deleting Documents in MongoDB with Examples. Please read our previous article where we discussed Replace Document in MongoDB with Examples.
Deleting Document in MongoDB
MongoDB documents are also known as BSON documents that are used to store data. The document is created using field-value pairs or key-value pairs and the value is of any BSON type. Here BSON document means the binary representation of JSON document.
To remove a document from the MongoDB Database we use the delete operation. It will delete an existing document from the specified collection. To delete a document, we can use any of the following methods:
db.collection.deleteOne()
db.collection.deleteMany()
Removing Single Document in MongoDB
db.collection.deleteOne() is used to delete the first document or a single document that matches the given filter.
- It removes the first document that matches the given filter.
- If we use this function in the capped collection, then it will return the WriteError exception.
- If we use this function in the time series collection, then it will return the WriteError exception.
- If you use this function in the shared collection, then it is necessary to include the shared key in the query specification.
- You can use this function inside the multi-document transaction.
- It deletes only one document at a time.
Syntax: db.collection.deleteOne(<Filter>, {writeConcern: <document>, collation:<document>, hint:<document|string>})
Parameter:
This function takes the following parameters:
- Filter: It represents the deletion criteria. If the value of this field is the empty document({}), then it will delete the very first document of the collection.
- writeConcern: It is an optional parameter. It removes the use of default write concern.
- collation: It is an optional parameter. It is used to specify the collation used for the operation.
- hint: It is an optional parameter. It is a document or a string that is used to specify the index to use to support the filter.
Return Value:
This function will return a document that contains the following fields with values:
- acknowledged: The value of this field is true when the insert operation ran with write concern. Otherwise, it will return false.
- deleteCount: It represents the total number of deleted documents.
Sample Database
In the below examples we use the following database:
Example – Delete Single Document in MongoDB
Here in the below example, we delete the first document of the company collection.
db.company.deleteOne({})
Example – Delete a document according to the filter
Here in the below example, we delete the first document that matches the filter which is Group: “C”.
db.company.deleteOne({Group:”C”})
Removing Multiple Documents in MongoDB
db.collection.deleteMany() is used to delete multiple existing documents from the specified collection.
- If we use this function without any filter document, then it will delete all the documents present in the given collection.
- If we use this function in the capped collection, then it will return the WriteError exception.
- If we use this function in the time series collection, then it will return the WriteError exception.
- If you use this function in the shared collection, then it is necessary to include the shared key in the query specification.
- You can use this function inside multi-document transactions.
Syntax: db.collection.deleteMany(<Filter>, {writeConcern: <document>, collation:<document>})
Parameter:
This function takes the following parameters:
- Filter: It represents the deletion criteria. If the value of this field is the empty document({}), then it will delete the very first document of the collection.
- writeConcern: It is an optional parameter. It removes the use of default write concern.
- collation: It is an optional parameter. It is used to specify the collation used for the operation.
Return value:
This function will return a document that contains the following fields with values:
- acknowledged: The value of this field is true when the insert operation ran with write concern. Otherwise, it will return false.
- deleteCount: It represents the total number of deleted documents.
Sample Database
In the below examples we use the following database:
Example – Deleting all the documents that match the given filter in MongoDB
Here in the below example, we delete all the documents that match the given filter which is Division:34.
db.company.deleteMany({Division:34})
Example 2 – Deleting All Documents in MongoDB
Here, in the below example, we delete all the documents from the given collection.
db.company.deleteMany({})
In the next article, I am going to discuss Embedded Documents in MongoDB with Examples. Here, in this article, I try to explain Deleting Documents in MongoDB with Examples. I hope you enjoy this Deleting Documents in MongoDB with Examples article.