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

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

MongoDB: updateMany: Update all the documents

Passing filter criteria as empty object, you can match to all the documents.

 

Example

db.employee.updateMany({}, {$set : {"active" : true, "modifiedTime": Date.now()}})

 

Let’s experiment with employee collection.

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

 

Set active flag to true and modifiedTime to current time stamp for all the documents in the collection.

> db.employee.updateMany({}, {$set : {"active" : true, "modifiedTime": Date.now()}})
{ "acknowledged" : true, "matchedCount" : 4, "modifiedCount" : 4 }
> 
> 
> 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
}

 

 

 

 

Previous                                                    Next                                                    Home

MongoDB: updateMany: Update multiple documents

‘updateMany()’ method is used to update all the documents that satisfy given filter criteria.

 

Syntax

db.collection.updateMany(
   <filter>,
   <update>,
   {
     upsert: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     arrayFilters: [ <filterdocument1>, ... ],
     hint:  <document|string>        // Available starting in MongoDB 4.2.1
   }
)

 

Example

db.employee.updateMany({"lastName" : "Gurram"}, {$set : {"active": false, "modifiedTime" : Date.now()}})

 

Let’s experiment with employee collection.

> db.employee.find()
{ "_id" : ObjectId("60baff65baf44d88348459bf"), "firstName" : "Krishna", "lastName" : "Gurram", "active" : false, "modifiedTime" : 1622868001073 }
{ "_id" : ObjectId("60baff6dbaf44d88348459c0"), "firstName" : "Ram", "lastName" : "Gurram", "active" : true }
{ "_id" : ObjectId("60baff75baf44d88348459c1"), "firstName" : "Thulasi", "lastName" : "Gurram", "active" : true }
{ "_id" : ObjectId("60baff83baf44d88348459c2"), "firstName" : "Gopi", "lastName" : "Battu", "active" : true }
{ "_id" : ObjectId("60baff8fbaf44d88348459c3"), "firstName" : "Sailaja", "lastName" : "PTR", "active" : true }

 

For example, set the active property of all the employees whose lastName is Gurram to false and add modifiedTime property with current time stamp.

> db.employee.updateMany({"lastName" : "Gurram"}, {$set : {"active": false, "modifiedTime" : Date.now()}})
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> 
>
>
> db.employee.find()
{ "_id" : ObjectId("60baff65baf44d88348459bf"), "firstName" : "Krishna", "lastName" : "Gurram", "active" : false, "modifiedTime" : 1622868537108 }
{ "_id" : ObjectId("60baff6dbaf44d88348459c0"), "firstName" : "Ram", "lastName" : "Gurram", "active" : false, "modifiedTime" : 1622868537108 }
{ "_id" : ObjectId("60baff75baf44d88348459c1"), "firstName" : "Thulasi", "lastName" : "Gurram", "active" : false, "modifiedTime" : 1622868537108 }
{ "_id" : ObjectId("60baff83baf44d88348459c2"), "firstName" : "Gopi", "lastName" : "Battu", "active" : true }
{ "_id" : ObjectId("60baff8fbaf44d88348459c3"), "firstName" : "Sailaja", "lastName" : "PTR", "active" : true }

 

 

 

 

 

 

Previous                                                    Next                                                    Home

MongoDB: updateOne: Update one document

Update one document that satisfies the filter criteria.

 

Syntax

db.collection.updateOne(
   <filter>,
   <update>,
   {
     upsert: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     arrayFilters: [ <filterdocument1>, ... ],
     hint:  <document|string>        // Available starting in MongoDB 4.2.1
   }
)

Example

db.employee.updateOne({"active" : true}, {$set : {"active" : false, "modifiedTime" : Date.now()}})

Above snippet update one matching document.

 

Actual Data

> db.employee.find().pretty()
{
      "_id" : ObjectId("60baff65baf44d88348459bf"),
      "firstName" : "Krishna",
      "lastName" : "Gurram",
      "active" : true
}
{
      "_id" : ObjectId("60baff6dbaf44d88348459c0"),
      "firstName" : "Ram",
      "lastName" : "Gurram",
      "active" : true
}
{
      "_id" : ObjectId("60baff75baf44d88348459c1"),
      "firstName" : "Thulasi",
      "lastName" : "Gurram",
      "active" : true
}
{
      "_id" : ObjectId("60baff83baf44d88348459c2"),
      "firstName" : "Gopi",
      "lastName" : "Battu",
      "active" : true
}
{
      "_id" : ObjectId("60baff8fbaf44d88348459c3"),
      "firstName" : "Sailaja",
      "lastName" : "PTR",
      "active" : true
}


After executing updateOne query

> db.employee.updateOne({"active" : true}, {$set : {"active" : false, "modifiedTime" : Date.now()}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> 
> 
> db.employee.find().pretty()
{
      "_id" : ObjectId("60baff65baf44d88348459bf"),
      "firstName" : "Krishna",
      "lastName" : "Gurram",
      "active" : false,
      "modifiedTime" : 1622868001073
}
{
      "_id" : ObjectId("60baff6dbaf44d88348459c0"),
      "firstName" : "Ram",
      "lastName" : "Gurram",
      "active" : true
}
{
      "_id" : ObjectId("60baff75baf44d88348459c1"),
      "firstName" : "Thulasi",
      "lastName" : "Gurram",
      "active" : true
}
{
      "_id" : ObjectId("60baff83baf44d88348459c2"),
      "firstName" : "Gopi",
      "lastName" : "Battu",
      "active" : true
}
{
      "_id" : ObjectId("60baff8fbaf44d88348459c3"),
      "firstName" : "Sailaja",
      "lastName" : "PTR",
      "active" : true
}





 

 

Previous                                                    Next                                                    Home

MongoDB: set the field with current timestamp in MongoDB

‘Date.now()’ function is used to set the field with current timestamp.

 

Example

db.audit.insert({"type" : "login", "client" : "mobile", "userId" : 123, "createdTimestamp": Date.now()})

 

Find the below snippet.

> db.audit.insert({"type" : "login", "client" : "mobile", "userId" : 123, "createdTimestamp": Date.now()})
WriteResult({ "nInserted" : 1 })
> 
> db.audit.find().pretty()
{
      "_id" : ObjectId("60bb0156baf44d88348459c4"),
      "type" : "login",
      "client" : "mobile",
      "userId" : 123,
      "createdTimestamp" : 1622868310979
}

 

 

 

Previous                                                    Next                                                    Home

MongoDB: insertMany: Insert multiple documents to the collection

‘insertMany’ method is used to insert multiple documents into the collection.

 

Signature

db.collection.insertMany(
   [ <document 1> , <document 2>, ... ],
   {
      writeConcern: <document>,
      ordered: <boolean>
   }
)

 

Example

db.employee.insert([{"firstName" : "Shankar", "lastName" : "nzv"}, {"firstName" : "Rohini", "lastName" : "Shankar"}])

 

Let’s experiment with employee collection.


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

 

Let’s insert 2 new employee documents.

> db.employee.insert([{"firstName" : "Shankar", "lastName" : "nzv"}, {"firstName" : "Rohini", "lastName" : "Shankar"}])
BulkWriteResult({
      "writeErrors" : [ ],
      "writeConcernErrors" : [ ],
      "nInserted" : 2,
      "nUpserted" : 0,
      "nMatched" : 0,
      "nModified" : 0,
      "nRemoved" : 0,
      "upserted" : [ ]
})
> 
> 
> 
> db.employee.find().pretty()
{
      "_id" : ObjectId("60bb035bbaf44d88348459c5"),
      "firstName" : "Sailja",
      "lastName" : "PTR",
      "active" : true,
      "modifiedTime" : 1622868890184
}
{
      "_id" : ObjectId("60bb062abaf44d88348459c6"),
      "firstName" : "Shankar",
      "lastName" : "nzv"
}
{
      "_id" : ObjectId("60bb062abaf44d88348459c7"),
      "firstName" : "Rohini",
      "lastName" : "Shankar"
}

 

 

 

Previous                                                    Next                                                    Home