MongoDB Remove Method (Day 10)
CBefore reading this article, I highly recommend reading the previous installments in the series:
- MongoDB - Day 1 (Introduction To MongoDB)
- MongoDB - Day 2 (Install MongoDB in Windows)
- MongoDB - Day 3 (Database Basics)
- MongoDB - Day 4 (Basics of Collection)
- MongoDB - Day 5 (Data Types in MongoDB)
- MongoDB - Day 6 (Insert Method)
- MongoDB - Day 7 (Find Method Part 1)
- MongoDB - Day 8 (Find Method Part
- MongoDB- Day 9(Update Method)
Remove() Method
The Remove method deletes (removes) document(s) from a collection. The Remove method searches documents based on the deleting-criteria (<query>) and removes all documents matching the criteria. We can remove a single item at a time using the <justOne> parameter, but we can't use the remove() method with a capped collection.
Syntax
Parameters
The Remove method deletes (removes) document(s) from a collection. The Remove method searches documents based on the deleting-criteria (<query>) and removes all documents matching the criteria. We can remove a single item at a time using the <justOne> parameter, but we can't use the remove() method with a capped collection.
Syntax
- db.Collection_Name.remove( query,{ justOne:<Boolean>} )
Parameter | Type | Description |
Query | Document | Specify the deletion criteria |
justone | Boolean | Optional, the default value is false. Remove a single document if set to true otherwise delete all the documents matching the deletion criteria. |
Return Type
The remove() returns an object containing the status of the operation. The remove() method returns a WriteResult object that contains the status of the operation. The WriteResult object is an integer type that returns the number of documents removed.
Now, we consider some examples and examine the behavior of the remove() method.
1. Create a collection
Code
- db.createCollection("Demo",{autoIndex:false,max:100,size:5040320})
![create collection](https://www.csharp.com/UploadFile/Tutorial/admin/mongodb-day10-remove-method29032020015257/Images/create collection.png)
Now insert some documents into the “Demo” collection.
- {
- "_id" : ObjectId("55ca1d327ad050ed6214a4e3"),
- "Product_Name" : "Pendrive",
- "Price" : 250
- "Amount" : 2
- }
- {
- "_id" : ObjectId("55ca1d547ad050ed6214a4e4"),
- "Product_Name" : "Book",
- "Price" : 350,
- "Amount" : 4
- }
- {
- "_id" : ObjectId("55ca1d6a7ad050ed6214a4e5"),
- "Product_Name" : "Photo Frame",
- "Price" : 150,
- "Amount" : 3
- }
- {
- "_id" : ObjectId("55ca1d947ad050ed6214a4e6"),
- "Product_Name" : "Pencil",
- "Price" : 15,
- "Amount" : 20
- }
- {
- "_id" : ObjectId("55ca1dc47ad050ed6214a4e7"),
- "Product_Name" : "Keyboard",
- "Price" : 1500,
- "Amount" : 4
- }
- {
- "_id" : ObjectId("55ca1dd57ad050ed6214a4e8"),
- "Product_Name" : "Mouse",
- "Price" : 125,
- "Amount" : 5
- }
![](https://www.csharp.com/UploadFile/Tutorial/admin/mongodb-day10-remove-method29032020015257/Images/Demo Collectiona.png)
Remove all document from collection
To remove all the documents in a collection, use the remove method with an empty query document {}.
Query
- db.Demo.remove({})
This query removes all the documents from the “Demo” collection.
![](https://www.csharp.com/UploadFile/Tutorial/admin/mongodb-day10-remove-method29032020015257/Images/Empty Collection.png)
Note that the remove() and drop() methods are not the same. The Drop() method drops an entire collection including its structure and index, but the remove() method deletes only the documents from the collection.
Remove all Documents that Match the Deletion Criteria
To remove all the documents that match a certain criteria, use the <query> parameter and apply the deletion criteria.
Query
- db.Demo.remove({Price:{$gt:235,$lt:2001}})
Collection after Query
![](https://www.csharp.com/UploadFile/Tutorial/admin/mongodb-day10-remove-method29032020015257/Images/Collection after Query.png)
Remove Single Document
To remove the first document that matches a deletion criteria, use the “justOne” parameter of the remove() method. Set the value of the “justOne” parameter to true or 1.
Query
- db.Demo.remove({Price:{$gt:235,$lt:2001}},{justOne:1})
Collection after Query
![](https://www.csharp.com/UploadFile/Tutorial/admin/mongodb-day10-remove-method29032020015257/Images/QueryPlus.png)
Remove method with Capped Collection
We can't delete a specific document from a capped collection. To remove all documents from a collection, use the drop() method to drop the collection.
Let's see an example:
![Example](https://www.csharp.com/UploadFile/Tutorial/admin/mongodb-day10-remove-method29032020015257/Images/Example.png)
In the preceding example, we tried to use the remove method with a capped collection so MongoDB throws an error. To remove all documents from a collection, use the drop() method.
Use Isolated option with remove operation
During the removal of multiple documents, the remove operation may interleave with other read and write operations of the collection. To override this behavior, use the “$isolated” option. The isolated option ensures that no client can see the affected documents until they are all processed or an error stops the removal operation. To use the isolate behavior in the remove method, set the “$isolated” parameter to 1 or true.
Example
![](https://www.csharp.com/UploadFile/Tutorial/admin/mongodb-day10-remove-method29032020015257/Images/Output.png)
Today, we learned about the remove method. In next the article, I will explain another method. Until then keep coding.
Next Article >> MongoDB Collection Methods (Day 11)
Author
Pankaj Kumar Choudhary
72
26.6k
13.5m