Before reading this article, I highly recommend reading the following previous parts of the series:
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 and can't use the remove() method with a capped collection.
Syntax
- db.Collection_Name.remove( query,{ justOne:<Boolean>} )
Parameters
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 containinig 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.
First we create a collection
Code
- db.createCollection("Demo",{autoIndex:false,max:100,size:5040320})
ResultNow 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
- }
Structure of “Demo” collection
Remove all document from collectionTo remove all the documents in a collection, use the remove method with an empty query document {}.
Query
Collection after Query
This query removes all the documents from the “Demo” collection.
Note that the remove() and drop() methods are not the same. The Drop() method drops an entire collection including it's structure and index but the remove() method deletes only the documents from the collection.
Remove all Documents that Match the Deletion CriteriaTo 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}})
This query removes all the documents in which the value of the “Price” field is greater than 350 and less than 2001.
Collection after QueryRemove 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})
This removes the first document that matches the deletion criteria.
Collection after Query
Remove method with Capped CollectionWe 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 us see an example as in the following:
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 operationDuring 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. Isolated option ensures that no client can see the affected documents until they are all processed or an error stops the remove operation. To use the isolate behavior in the remove method set the “$isolated” parameter to 1 or true.
ExampleToday we learned about the remove method. In next the article I will explain another method. Until then keep coding.