Friday, 20 February 2015

mongodb : remove(): Delete document


“remove” method is used to delete documents from a collection.

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

Parameter
Type
Description
query
document
Specifies deletion criteria using query operators. To delete all documents in a collection, pass an empty document ({}).
justOne
boolean
Optional. 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.
writeConcern
document
Optional. A document expressing the write concern. Omit to use the default write concern


> db.employee.find()
{ "_id" : ObjectId("54b2a8c9dbf1847bed465758"), "id" : 1, "firstName" : "Hari Krishna", "lastName" : "Gurram" }
{ "_id" : 2, "firstName" : "Rama Krishna", "lastName" : "Gurram" }
{ "_id" : ObjectId("54b2aa07dbf1847bed465759"), "id" : 1, "firstName" : "Jessi", "lastName" : "chelli" }
{ "_id" : ObjectId("54b2aa07dbf1847bed46575a"), "id" : 2, "firstName" : "Ananad", "lastName" : "Bandaru" }
{ "_id" : ObjectId("54b2aa07dbf1847bed46575b"), "id" : 3, "firstName" : "Gopi", "lastName" : "Battu" }
{ "_id" : ObjectId("54b2aa07dbf1847bed46575c"), "id" : 4, "firstName" : "Ritwik", "lastName" : "Mohenthy" }
>
>
> db.employee.remove({ id: { $gt: 1 } })
WriteResult({ "nRemoved" : 3 })
>
>
> db.employee.find()
{ "_id" : ObjectId("54b2a8c9dbf1847bed465758"), "id" : 1, "firstName" : "Hari Krishna", "lastName" : "Gurram" }
{ "_id" : 2, "firstName" : "Rama Krishna", "lastName" : "Gurram" }
{ "_id" : ObjectId("54b2aa07dbf1847bed465759"), "id" : 1, "firstName" : "Jessi", "lastName" : "chelli" }
>


To remove all documents
db.employee.remove({})” remove all documents from collection employee.

> db.employee.find()
{ "_id" : ObjectId("54b2a8c9dbf1847bed465758"), "id" : 1, "firstName" : "Hari Krishna", "lastName" : "Gurram" }
{ "_id" : 2, "firstName" : "Rama Krishna", "lastName" : "Gurram" }
{ "_id" : ObjectId("54b2aa07dbf1847bed465759"), "id" : 1, "firstName" : "Jessi", "lastName" : "chelli" }
>
>
> db.employee.remove({})
WriteResult({ "nRemoved" : 3 })
>
> db.employee.find()
>

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment