Update Query in mangoDB
Update one Document
UpdateOne()=>db.COLLECTION_NAME.updateOne(<filter>,<update>)Update multiple Document
UpdateMany()=>db.COLLECTION_NAME.updateMany(<filter>,<update>)1. Update the JavaScript type value to "Full Stack".
> show dbs admin 0.000GB config 0.000GB local 0.000GB shreyashdb 0.000GB
> use shreyashdb switched to db shreyashdb
> db shreyashdb
> show collections shreyashdb
> db.shreyashdb.find().pretty() { "_id" : ObjectId("61ca837682bfb31fe50836eb"), "name" : "ReactJS", "type" : "Front End", "blogpost" : 100, "active" : true } { "_id" : ObjectId("61ca89a182bfb31fe50836ec"), "name" : "shreyash kolhe", "email" : "shreyashkolhe2001@gmail.com", "role" : "developer", "active" : true }The $set operator replaces the value of a field with the specified value.
> db.shreyashdb.updateOne({name:"ReactJS"}, {$set: {type: "Full Stack"}}) { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.shreyashdb.find().pretty() { "_id" : ObjectId("61ca837682bfb31fe50836eb"), "name" : "ReactJS", "type" : "Full Stack", "blogpost" : 100, "active" : true } { "_id" : ObjectId("61ca89a182bfb31fe50836ec"), "name" : "shreyash kolhe", "email" : "shreyashkolhe2001@gmail.com", "role" : "developer", "active" : true }2. Update all the fields with the type value equal to "Front End" and set the value of email to shreyashkolhe2001@gmail.com.
> show dbs admin 0.000GB config 0.000GB local 0.000GB shreyashdb 0.000GB test 0.000GB
> show collections test
> db.test.insertMany([{name:"shreyash",sirname:"kolhe",role:"front-end",email:"skd@gmail.com"}, {name:"vabhav",sirname:"bandal",role:"front-end",email:"skd@gmail.com"},{name:"shubham",sirname:"gawali",role:"backend",email:"skd@gmail.com"},{name:"xyh",sirname:"xyz",role:"front-end",email:"skd@gmail.com"}]) { "acknowledged" : true, "insertedIds" : [ ObjectId("61cabfab23c54413fe585b4b"), ObjectId("61cabfab23c54413fe585b4c"), ObjectId("61cabfab23c54413fe585b4d"), ObjectId("61cabfab23c54413fe585b4e") ] }
> show dbs admin 0.000GB config 0.000GB local 0.000GB shreyashdb 0.000GB test 0.000GB
> show collections test
> db.test.find().pretty() { "_id" : ObjectId("61cabea123c54413fe585b4a"), "name" : "shreyash", "email" : "sk@gmail.com" } { "_id" : ObjectId("61cabfab23c54413fe585b4b"), "name" : "shreyash", "sirname" : "kolhe", "role" : "front-end", "email" : "skd@gmail.com" } { "_id" : ObjectId("61cabfab23c54413fe585b4c"), "name" : "vabhav", "sirname" : "bandal", "role" : "front-end", "email" : "skd@gmail.com" } { "_id" : ObjectId("61cabfab23c54413fe585b4d"), "name" : "shubham", "sirname" : "gawali", "role" : "backend", "email" : "skd@gmail.com" } { "_id" : ObjectId("61cabfab23c54413fe585b4e"), "name" : "xyh", "sirname" : "xyz", "role" : "front-end", "email" : "skd@gmail.com" }
> db.test.updateMany({role:"front-end"}, {$set: {email:"shreyashkolhe2001@gmail.com"}}) { "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.test.find().pretty()
{
"_id" : ObjectId("61cabea123c54413fe585b4a"),
"name" : "shreyash",
"email" : "sk@gmail.com"
}
{
"_id" : ObjectId("61cabfab23c54413fe585b4b"),
"name" : "shreyash",
"sirname" : "kolhe",
"role" : "front-end",
"email" : "shreyashkolhe2001@gmail.com"
}
{
"_id" : ObjectId("61cabfab23c54413fe585b4c"),
"name" : "vabhav",
"sirname" : "bandal",
"role" : "front-end",
"email" : "shreyashkolhe2001@gmail.com"
}
{
"_id" : ObjectId("61cabfab23c54413fe585b4d"),
"name" : "shubham",
"sirname" : "gawali",
"role" : "backend",
"email" : "skd@gmail.com"
}
{
"_id" : ObjectId("61cabfab23c54413fe585b4e"),
"name" : "xyh",
"sirname" : "xyz",
"role" : "front-end",
"email" : "shreyashkolhe2001@gmail.com"
}
>
Post a Comment