Saturday, 5 June 2021

MongoDB: update vs updateMany

 ‘updateMany’ is used to update all the documents that matches to the given filter, whereas ‘update’ method can modify specific fields of an existing document or documents or replace an existing document entirely, depending on the update parameter. Let’s see it with an example.

> db.audit.find().pretty()
{
	"_id" : ObjectId("60bb0156baf44d88348459c4"),
	"type" : "login",
	"client" : "mobile",
	"userId" : 123,
	"createdTimestamp" : 1622868310979
}
{
	"_id" : ObjectId("60bb9c348251689f64fb3ece"),
	"type" : "logout",
	"client" : "browser",
	"userId" : 234,
	"createdTimestamp" : 1622907956987
}
{
	"_id" : ObjectId("60bb9c418251689f64fb3ecf"),
	"type" : "login",
	"client" : "browser",
	"userId" : 1,
	"createdTimestamp" : 1622907969953
}
{
	"_id" : ObjectId("60bb9c598251689f64fb3ed0"),
	"type" : "reset_credentials",
	"client" : "mobile",
	"userId" : 31,
	"createdTimestamp" : 1622907993918
}
{
	"_id" : ObjectId("60bb9c6f8251689f64fb3ed1"),
	"type" : "login",
	"client" : "mobile",
	"userId" : 41,
	"createdTimestamp" : 1622908015960
}
>

Update parameter like $set is mandatory for updateMany method.

 

For example,

> db.audit.updateMany({"type" : "login"}, {"type" : "signIn", "modifiedTimestamp" : Date.now()})
uncaught exception: Error: the update operation document must contain atomic operators :
DBCollection.prototype.updateMany@src/mongo/shell/crud_api.js:655:19
@(shell):1:1


Try using $set parameter like below.

db.audit.updateMany({"type" : "login"}, {$set : {"type" : "signIn", "modifiedTimestamp" : Date.now()}})

> db.audit.updateMany({"type" : "login"}, {$set : {"type" : "signIn", "modifiedTimestamp" : Date.now()}})
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> 
> db.audit.find().pretty()
{
	"_id" : ObjectId("60bb0156baf44d88348459c4"),
	"type" : "signIn",
	"client" : "mobile",
	"userId" : 123,
	"createdTimestamp" : 1622868310979,
	"modifiedTimestamp" : 1622908523246
}
{
	"_id" : ObjectId("60bb9c348251689f64fb3ece"),
	"type" : "logout",
	"client" : "browser",
	"userId" : 234,
	"createdTimestamp" : 1622907956987
}
{
	"_id" : ObjectId("60bb9c418251689f64fb3ecf"),
	"type" : "signIn",
	"client" : "browser",
	"userId" : 1,
	"createdTimestamp" : 1622907969953,
	"modifiedTimestamp" : 1622908523246
}
{
	"_id" : ObjectId("60bb9c598251689f64fb3ed0"),
	"type" : "reset_credentials",
	"client" : "mobile",
	"userId" : 31,
	"createdTimestamp" : 1622907993918
}
{
	"_id" : ObjectId("60bb9c6f8251689f64fb3ed1"),
	"type" : "signIn",
	"client" : "mobile",
	"userId" : 41,
	"createdTimestamp" : 1622908015960,
	"modifiedTimestamp" : 1622908523246
}


If you do not provide update parameter ($set), update method replaces one matched document with the new document.

> db.audit.update({"type" : "signIn"}, {"type" : "login", "modifiedTimestamp" : Date.now()})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> 
> db.audit.find().pretty()
{
	"_id" : ObjectId("60bb0156baf44d88348459c4"),
	"type" : "login",
	"modifiedTimestamp" : 1622908612185
}
{
	"_id" : ObjectId("60bb9c348251689f64fb3ece"),
	"type" : "logout",
	"client" : "browser",
	"userId" : 234,
	"createdTimestamp" : 1622907956987
}
{
	"_id" : ObjectId("60bb9c418251689f64fb3ecf"),
	"type" : "signIn",
	"client" : "browser",
	"userId" : 1,
	"createdTimestamp" : 1622907969953,
	"modifiedTimestamp" : 1622908523246
}
{
	"_id" : ObjectId("60bb9c598251689f64fb3ed0"),
	"type" : "reset_credentials",
	"client" : "mobile",
	"userId" : 31,
	"createdTimestamp" : 1622907993918
}
{
	"_id" : ObjectId("60bb9c6f8251689f64fb3ed1"),
	"type" : "signIn",
	"client" : "mobile",
	"userId" : 41,
	"createdTimestamp" : 1622908015960,
	"modifiedTimestamp" : 1622908523246
}


Let’s execute same update query again.

> db.audit.update({"type" : "signIn"}, {"type" : "login", "modifiedTimestamp" : Date.now()})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> 
> db.audit.find().pretty()
{
	"_id" : ObjectId("60bb0156baf44d88348459c4"),
	"type" : "login",
	"modifiedTimestamp" : 1622908612185
}
{
	"_id" : ObjectId("60bb9c348251689f64fb3ece"),
	"type" : "logout",
	"client" : "browser",
	"userId" : 234,
	"createdTimestamp" : 1622907956987
}
{
	"_id" : ObjectId("60bb9c418251689f64fb3ecf"),
	"type" : "login",
	"modifiedTimestamp" : 1622908670676
}
{
	"_id" : ObjectId("60bb9c598251689f64fb3ed0"),
	"type" : "reset_credentials",
	"client" : "mobile",
	"userId" : 31,
	"createdTimestamp" : 1622907993918
}
{
	"_id" : ObjectId("60bb9c6f8251689f64fb3ed1"),
	"type" : "signIn",
	"client" : "mobile",
	"userId" : 41,
	"createdTimestamp" : 1622908015960,
	"modifiedTimestamp" : 1622908523246
}


If you provide $set update parameter, it updates the documents, instead of replacing.

> db.audit.update({"type" : "signIn"}, {$set : {"type" : "login", "modifiedTimestamp" : Date.now()}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> 
> db.audit.find().pretty()
{
	"_id" : ObjectId("60bb0156baf44d88348459c4"),
	"type" : "login",
	"modifiedTimestamp" : 1622908612185
}
{
	"_id" : ObjectId("60bb9c348251689f64fb3ece"),
	"type" : "logout",
	"client" : "browser",
	"userId" : 234,
	"createdTimestamp" : 1622907956987
}
{
	"_id" : ObjectId("60bb9c418251689f64fb3ecf"),
	"type" : "login",
	"modifiedTimestamp" : 1622908670676
}
{
	"_id" : ObjectId("60bb9c598251689f64fb3ed0"),
	"type" : "reset_credentials",
	"client" : "mobile",
	"userId" : 31,
	"createdTimestamp" : 1622907993918
}
{
	"_id" : ObjectId("60bb9c6f8251689f64fb3ed1"),
	"type" : "login",
	"client" : "mobile",
	"userId" : 41,
	"createdTimestamp" : 1622908015960,
	"modifiedTimestamp" : 1622908909572
}










 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment