Introduction to MongoDB Delete
MongoDB Delete method is an essential method, used to delete or remove a collection or a document existing in a MongoDB Collection. Upon creation of a document or updating an existing document, we might encounter a situation where we might have to delete some records, this is where Delete method can be implemented. MongoDB’s Delete methods are shell commands, which allow a user to delete or remove the records or documents upon insertion and if not needed. There are multiple programming languages that supports these methods to delete records with APIs.
Syntax:
There are two basic ways to implement Delete Method, which are deleteOne method and deleteMany method. Every method has its functions and applicability, based on which it can be used.
1. Syntax for deleteOne method
db.collection.deleteOne( < filter > )
Explanation:
- Simply removes a single document from a collection, which matches the mentioned filter. Here, collection specifies the name of the collection from which we intend to delete the record.
- Then, deleteOne specifies that only one record must be deleted and the filter, is the criteria based on which, the records to be deleted must be selected.
2. Syntax for deleteMany method
db.collection.deleteMany( < filter > )
Explanation:
- Removes total number of documents that matches with the filter mentioned. Just like deleteOne, collection holds the same place, but the deleteMany, specifies that the number of records to be deleted can be more than one.
- Filter, helps in selecting the documents, which matches the criteria mentioned and then it selects the number of records that matches, which can be more than one and it deletes the total number of records selected.
Refer the below screenshot of help docs on MongoDB Shell:
Output:
As you can see in the above help, both methods takes optional parameters. Followed by the working of the method, respectively.
Syntax for Delete without filter or parameters:
Here we have seen delete document, with single or multiple documents and how to add filters to make sure specific documents are deleted. Now, if we want to delete every single document inside a collection, we will pass simply empty filter, which is, {}.
db.locs.deleteMany( {} )
How Delete Command works in MongoDB?
- Specifically, the deleteOne method will delete the very first document, which matches with the mentioned filter. For precious deletion, using the field with unique index like “_id” could work in a better way.
- With the working of the Delete Command, there is an execution, which occurs only in case of capped collections, which are collections with fixed size and will overwrites its oldest records when it reaches maximum set limit. Now to remove such capped collections and proceed with normal working, it is recommended to implement the db.collection.drop() method.
- Working on remove method is similar to as of delete(), but it differs only with the return values. When deleted with delete method, return will be the acknowledgment of the deletion and number of records deleted, while with remove method, write result is return with number of records removed.
Examples of MongoDB Delete
Given below are the examples mentioned:
Example #1 – deleteOne
Code:
db.code.deleteOne({"name":"malti"})
Output:
Explanation:
- Here we attempt to delete a single record, which matches with mentioned key value pair. To start with, code is our collection in which the document might or might not exist. Then we have our method which is deleteOne and then we have the filter mentioned inside. Here, our filter is that it should look for document which has the key as “name” and the value must match to “malti”.
- Upon finding a document which matches the filter, the method will delete the document. As you can see, we implemented the deleteOne method and then when we listed the whole collection, we now don’t have any record or a document with the name as malti.
Example #2 – deleteMany
Code:
db.code.find()
db.code.deleteMany({"city":"Pune"})
Output:
Explanation:
- Started with db, with collection name, we have our deleteMany method, which will delete multiple documents in the code collection. It will rely on the filter mentioned to delete these documents. Our filter is “{“city”:”Pune”}”, meaning it will delete every document that has the city key, matching with the value of Pune.
- Executing this query, every document present in the collection “code”, with the Pune as city, will be deleted at once. As you can see, we implemented the deleteMany method with filter and then when we returned the whole collection, which is now empty. Initially we had two documents with city as Pune, but after executing our query, there are no documents with city as Pune. This is how deleteMany deletes every record that matches the filter.
Example #3 – Complete Deletion
A delete method which deletes every single record available in the collection, at once. By simply not specifying any filter, we attempt to delete every single record stored in the collection.
4.5 (4,805 ratings)
View Course
Code:
db.locs.deleteMany( {} )
db.code.find().count()
Output:
Explanation:
- As you can see in the above screenshot, we firstly checked the total count of records in the collection, which was 195. Then we executed the deleteMany query with blank filter, which deleted every single record available.
- Which resulted in emptying the whole collection. Later upon checking for the count, we get 0 as result, meaning no record. That’s how deleteMany with no filter works.
Conclusion
To wrap it up, delete is an important function for MongoDB and can be used in two ways. It simply removes the document that matches the filter. It can be used to delete one document at once or many documents altogether. We understood the working of each method followed by the examples and screenshot. Delete methods are used to remove records from the collection, deleteOne deletes the first record that matches the filter and deleteMany removes every record that matches the filter.
Recommended Articles
This is a guide to MongoDB Delete. Here we discuss the introduction, how delete command works in MongoDB and examples. You may also have a look at the following articles to learn more –