https://www.tutorialspoint.com/mongodb/mongodb_query_document.htm
https://www.tutorialspoint.com/mongodb/mongodb_update_document.htm
The remove() collection method will delete documents that match the provided query.
db.potions.remove( {"name": "Love"} )
WriteResult({ "nRemoved": 1 })
is atomic . so no 2 clients may update the same document
Syntax : db.foo.update(query , update , options);
foo is the collection name , "query" for targeted document/documents , "update" parameter to mention what change you want to make
options like upsert , one-many ,
db.foo.update({a:1} , {$inc : {a:1}})
db.potions.update( {"name": "Love"}, {"$set": {"price": 3.99 }} )
WriteResult({
"nMatched": 1,
"nUpserted": 0,
"nModified": 1
})
If the update parameter consists of only field/value pairs, then everything but the _id is replaced in the matching document.
db.potions.update( {"name": "Love"}, {"price": 3.99 } )
{"multi": true}
db.potions.update( {"vendor": "KC"}, {"$set": { "vendor": "Kettlecooked" }} {"multi": true} )
We can use the $inc operator to increment the count of an existing log document.
db.logs.update( {"potion": "Shrinking"}, {"$inc": {"count": 1}} )
If field doesn’t exist, it gets created with the value.
db.logs.update( {"potion": "Love"}, {"$inc": {"count": 1}}, {"upsert": true} )
WriteResult({ "nMatched": 0, "nUpserted": 1, "nModified": 0 })
Running same query again will result in :
WriteResult({ "nMatched": 1, "nUpserted": 0, "nModified": 1
})
db.a.update({_id :1} , {$unset :{y: 0}})
db.foo.update({_id :1} , {$rename: {'Naem' : 'Name'}})
Unset field : color db.potions.update( {}, {"$unset": {"color": ""}}, {"multi": true} )
We can use $rename to change field names.
{"$rename": {fieldToRename: newField}}
db.potions.update( {}, {"$rename": {"score": "grade"}}, {"multi": true} )
"ingredients": ["hippo", "secret", "mouse feet"]
db.potions.update( {"ingredients": "secret"}, {"$set": {"ingredients": "42"}} )
Since array values are treated individually, we can update a single value by specifying its location in the array using dot notation.
ingredients.1 : Means 2nd array element
db.potions.update( {"name": "Shrinking"}, {"$set": {"ingredients.1" : 42}} )
db.potions.update( {"ingredients": "secret"}, {"$set": {"ingredients.$" : 42}}, {"multi": true} )
"ratings": {
"strength": 1, "flavor": 5 }
db.potions.update( {"name": "Shrinking"}, {"$set": {"ratings.strength" : 5}} )
$max , $min , $mul
"categories": ["tasty", "effective"]
db.potions.update( {"name": "Shrinking"}, {"$pop": {"categories": 1}})
Result "categories": ["tasty"]
The $push operator will add a value to the end of an array.
{"_id" :1 , "things" :["one"] }
db.foo.update({_id :1} , { $push : {things : "two"} })
The $addToSet operator will add a value to the end of an array unless it is already present.
The $pull operator will remove any instance of a value from an array. i.e if there are 2 elements in an array with name "three" , then pull will remove all instances of three
{$pull : {things : 'three'}}
$addToSet
db.foo.update({} , {$push : {things : 4}})
{} means match any . bydefault only one document is affected.
db.foo.update({} , {$push : {things : 4}} , {multi : true })