Friday, 4 June 2021

MongoDB: deleteOne: Delete one document

‘deleteOne’ method deletes single document that matches to given filter criteria.

 

Signature

db.collection.deleteOne(
   <filter>,
   {
      writeConcern: <document>,
      collation: <document>,
      hint: <document|string>        // Available starting in MongoDB 4.4
   }
)

 

Example

db.employee.deleteOne({"active": true})

 

Let’s experiment with employee collection.

> db.employee.find().pretty()
{
      "_id" : ObjectId("60baff65baf44d88348459bf"),
      "firstName" : "Krishna",
      "lastName" : "Gurram",
      "active" : true,
      "modifiedTime" : 1622868890184
}
{
      "_id" : ObjectId("60baff6dbaf44d88348459c0"),
      "firstName" : "Ram",
      "lastName" : "Gurram",
      "active" : true,
      "modifiedTime" : 1622868890184
}
{
      "_id" : ObjectId("60baff75baf44d88348459c1"),
      "firstName" : "Thulasi",
      "lastName" : "Gurram",
      "active" : true,
      "modifiedTime" : 1622868890184
}
{
      "_id" : ObjectId("60bb035bbaf44d88348459c5"),
      "firstName" : "Sailja",
      "lastName" : "PTR",
      "active" : true,
      "modifiedTime" : 1622868890184
}

 

Delete one active employee.

> db.employee.deleteOne({"active": true})
{ "acknowledged" : true, "deletedCount" : 1 }
> 
> 
> db.employee.find().pretty()
{
      "_id" : ObjectId("60baff6dbaf44d88348459c0"),
      "firstName" : "Ram",
      "lastName" : "Gurram",
      "active" : true,
      "modifiedTime" : 1622868890184
}
{
      "_id" : ObjectId("60baff75baf44d88348459c1"),
      "firstName" : "Thulasi",
      "lastName" : "Gurram",
      "active" : true,
      "modifiedTime" : 1622868890184
}
{
      "_id" : ObjectId("60bb035bbaf44d88348459c5"),
      "firstName" : "Sailja",
      "lastName" : "PTR",
      "active" : true,
      "modifiedTime" : 1622868890184
}

 

 

 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment