w3resource

MongoDB: db.collection.remove() method

db.collection.remove

The db.collection.remove() method is used to remove documents from a collection.

Syntax:

db.collection.remove()

The db.collection.remove() method can have one of two syntaxes. The remove() method can take a query document and an optional justOne boolean:

db.collection.remove(
   <query>,
   <justOne>
)

Or the method can take a query document and an optional remove options document:

db.collection.remove(
   <query>,
   {
     justOne: <boolean>,
     writeConcern: <document>
   }
)

Paramater:

Name Description Required /
Optional
Type
query Specifies deletion criteria using query operators. To delete all documents in a collection, pass an empty document ({}). Required document
justOne To limit the deletion to just one document, set to true. Omit to use the default value of false and delete all documents matching the deletion criteria. Optional boolean
writeConcern A document expressing the write concern. Omit to use the default write concern. Optional document

Returns:

  • A WriteResult object that contains the status of the operation.

Sample document in the invoice collection:


{
        "_id" : ObjectId("5677d313fad7da08e362a3b6"),
        "inv_no" : "I00001",
        "inv_date" : "10/10/2012",
        "ord_qty" : 200
}
{
        "_id" : 901,
        "inv_no" : "I00001",
        "inv_date" : "10/10/2012",
        "ord_qty" : 500
}
{ "_id" : 10, "item" : "box", "ord_qty" : 150 }

Example: Remove All Documents from a Collection

The following operation deletes all documents from thecollection invoice. This operation is not equivalent to thedrop()method.

db.invoice.remove( { } );

Output:

> db.invoice.remove( { } );
WriteResult({ "nRemoved" : 3 })

Example: Remove All Documents that Match a Condition

The following operation removes all the documents from the collectioninvoice whereqtyis less than 200:

db.invoice.remove( { ord_qty: { $lt: 200 } } );

Output:

> db.invoice.remove( { ord_qty: { $lt: 200 } } );
WriteResult({ "nRemoved" : 1 })

Now see the collection invoice after remove.

> db.invoice.find().pretty();
{
        "_id" : ObjectId("5677d313fad7da08e362a3b6"),
        "inv_no" : "I00001",
        "inv_date" : "10/10/2012",
        "ord_qty" : 200
}
{
        "_id" : 901,
        "inv_no" : "I00001",
        "inv_date" : "10/10/2012",
        "ord_qty" : 500
}

Example: Remove a Single Document that Matches a Condition

The following operation removes the first document from the collectioninvoicewhereord_qtyis greater than 100:

db.invoice.remove( { ord_qty: { $gt: 100 } }, true );

Output:

> db.invoice.remove( { ord_qty: { $gt: 100 } }, true );
WriteResult({ "nRemoved" : 1 })

Now see the collection invoice after remove.

> db.invoice.find().pretty();
> db.invoice.find().pretty();
{
        "_id" : 901,
        "inv_no" : "I00001",
        "inv_date" : "10/10/2012",
        "ord_qty" : 500
}
{ "_id" : 10, "item" : "box", "ord_qty" : 150 }

Retrieve the restaurants data from here

Behaviors:

Safe Writes

The remove() method uses the delete command, which uses the default write concern. To specify a different write concern, include the write concern in the options parameter.

Previous: db.collection.reIndex() method
Next: db.collection. renameCollection() method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mongodb/shell-methods/collection/db-collection-remove.php