MongoDB Remove Method (Day 10)

CBefore reading this article, I highly recommend reading the previous installments in 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, but we can't use the remove() method with a capped collection.

Syntax
  1. 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 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

  1. db.createCollection("Demo",{autoIndex:false,max:100,size:5040320})  
Result

create collection

Now insert some documents into the “Demo” collection.
  1. {  
  2.          "_id" : ObjectId("55ca1d327ad050ed6214a4e3"),  
  3.          "Product_Name" : "Pendrive",  
  4.          "Price" : 250  
  5.          "Amount" : 2  
  6.  }  
  7.  {  
  8.          "_id" : ObjectId("55ca1d547ad050ed6214a4e4"),  
  9.          "Product_Name" : "Book",  
  10.          "Price" : 350,  
  11.          "Amount" : 4  
  12.  }  
  13.  {  
  14.          "_id" : ObjectId("55ca1d6a7ad050ed6214a4e5"),  
  15.          "Product_Name" : "Photo Frame",  
  16.          "Price" : 150,  
  17.          "Amount" : 3  
  18.  }  
  19.  {  
  20.          "_id" : ObjectId("55ca1d947ad050ed6214a4e6"),  
  21.          "Product_Name" : "Pencil",  
  22.          "Price" : 15,  
  23.          "Amount" : 20  
  24.  }  
  25.  {  
  26.          "_id" : ObjectId("55ca1dc47ad050ed6214a4e7"),  
  27.          "Product_Name" : "Keyboard",  
  28.          "Price" : 1500,  
  29.          "Amount" : 4  
  30.  }  
  31.  {  
  32.          "_id" : ObjectId("55ca1dd57ad050ed6214a4e8"),  
  33.          "Product_Name" : "Mouse",  
  34.          "Price" : 125,  
  35.          "Amount" : 5  
  36.  }  
Structure of “Demo” collection



Remove all document from collection

To remove all the documents in a collection, use the remove method with an empty query document {}.

Query
  1. db.Demo.remove({})  
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 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
  1. db.Demo.remove({Price:{$gt:235,$lt:2001}})  
This query removes all the documents where the value of the “Price” field is greater than 350 and less than 2001.

Collection after Query



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
  1. 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 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

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



Today, we learned about the remove method. In next the article, I will explain another method. Until then keep coding.
 
Author
Pankaj Kumar Choudhary
70 26.6k 13.4m
Next » MongoDB Collection Methods (Day 11)