Friday 4 June 2021

MongoDB: deleteMany: Delete multiple documents

‘deleteMany’ method is used to delete multiple documents that matches to given filter criteria.

 

Signature

db.collection.deleteMany(
   <filter>,
   {
      writeConcern: <document>,
      collation: <document>
   }
)

Example

db.employee.deleteMany({"lastName" : "Gurram"})


Let’s experiment with employee collection.

> 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
}


Delete all the employees whose lastName is Gurram.

> db.employee.deleteMany({"lastName" : "Gurram"})
{ "acknowledged" : true, "deletedCount" : 2 }
> 
> db.employee.find().pretty()
{
      "_id" : ObjectId("60bb035bbaf44d88348459c5"),
      "firstName" : "Sailja",
      "lastName" : "PTR",
      "active" : true,
      "modifiedTime" : 1622868890184
}






 

Previous                                                    Next                                                    Home

No comments:

Post a Comment