MongoDB Update has two commands:
1). Update () command
Db.collection.update (criteria, objnew, Upsert, Multi)
Criteria:update query conditions, similar to those in the SQL update query
Objnew:update objects and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after the
Upsert: This parameter means that if there is no record of update, insert Objnew,true is inserted, default is False, not inserted.
Multi:mongodb default is False, only update the first record found, if this parameter is true, the condition is checked out all the records are updated.
Cases:
Db.test0.update ({"Count": {$gt: 1}}, {$set: {"test2": "OK"}}); Only the first record is updated
Db.test0.update ({"Count": {$gt: 3}}, {$set: {"test2": "OK"}},false,true); It's all updated.
Db.test0.update ({"Count": {$gt: 4}}, {$set: {"Test5": "OK"}},true,false); Only added the first one.
Db.test0.update ({"Count": {$gt: 5}}, {$set: {"Test5": "OK"}},true,true); All added in.
Db.test0.update ({"Count": {$gt: $}}, {$inc: {"Count": 1}},false,true), fully updated
Db.test0.update ({"Count": {$gt: Ten}}, {$inc: {"Count": 1}},false,false); Update only the first one
2). Save () command
Db.collection.save (x)
X is the object to be updated, only a single record.
If a record of the same "_id" as the X object already exists within the collection. MongoDB will replace the X object with the existing record in collection, otherwise the X object will be inserted, and if there is no _id within X, a re-insert will be generated automatically. Equivalent to the upsert=true,multi=false of the above UPDATE statement.
Cases:
Db.test0.save ({count:40,test1: "OK"}); #_id系统会生成
Db.test0.save ({_id:40,count:40,test1: "OK"}); #如果test0内有_id等于40的, will be replaced, otherwise inserted.
The update operator for MongoDB:
1) $inc
Usage: {$inc: {Field:value}}
Meaning to a Number field field adds value, for example:
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": +}, "Count": +, "test1": "Testtest", "test2": "OK", "test3": "Testtest", "test4": "OK" , "Test5": "OK"}
> db.test0.update ({"_id":}, {$inc: {"Count": 1}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": {"}", "Count": +, "test1": "Testtest", "test2": "OK", "test3": "Testtest", "test4": "OK" , "Test5": "OK"}
> db.test0.update ({"_id":}, {$inc: {"Count": 2}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox":}, "Count": +, "test1": "Testtest", "test2": "OK", "test3": "Testtest", "test4": "OK" , "Test5": "OK"}
> db.test0.update ({"_id":}, {$inc: {"Count":-1}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": {"}", "Count": "Test1": "Testtest", "test2": "OK", "test3": "Testtest", "test4": "OK" , "Test5": "OK"}
2) $set
Usage: {$set: {Field:value}}
is the equivalent of SQL set field = value, all data types support $set. Cases:
> db.test0.update ({"_id":}, {$set: {"test1": "TESTV1", "test2": "Testv2", "test3": "TESTV3", "test4": "Test V4 "}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count": "Test1": "TESTV1", "test2": "Testv2", "test3": "TESTV3", "test4": "tes TV4 "," Test5 ":" OK "}
3) $unset
Usage: {$unset: {field:1}}
As the name implies, the field is deleted. Cases:
> db.test0.update ({"_id":}, {$unset: {"test1": 1}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": {"}", "Count":, "test2": "Testv2", "test3": "TESTV3", "test4": "TESTV4", "Test5": "OK" }
> db.test0.update ({"_id":}, {$unset: {"test2": 0}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count": "Test3": "TESTV3", "test4": "TESTV4", "Test5": "OK"}
> db.test0.update ({"_id":}, {$unset: {"test3": asdfasf}});
Fri 16:17:38 JS Error:ReferenceError:asdfasf is not defined (shell): 0
> db.test0.update ({"_id":}, {$unset: {"test3": "Test"}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": +}, "Count": "Test4": "TESTV4", "Test5": "OK"}
Do not see field:1 inside of 1 is what to use, anyway as long as there is something on the line.
4) $push
Usage: {$push: {Field:value}}
Append value To field, field must be array type, if field does not exist, a new array type will be added. Cases:
> db.test0.update ({"_id":}, {$set: {"test1": ["AAA", "BBB"]}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count":, "test1": ["AAA", "BBB"], "test4": "TESTV4", "Test5": "OK"}
> db.test0.update ({"_id":}, {$push: {"test1": "CCC"}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": {"}", "Count":, "test1": ["AAA", "BBB", "CCC"], "test4": "TESTV4", "Test5": "OK"}
> db.test0.update ({"_id":}, {$push: {"test2": "CCC"}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count":, "test1": ["AAA", "BBB", "CCC"], "test2": ["CCC"], "test4": "TESTV4" , "Test5": "OK"}
> db.test0.update ({"_id":}, {$push: {"test1": ["ddd", "Eee"]}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "count": +, "test1": ["AAA", "BBB", "CCC", ["ddd", "Eee"]], "test2": ["CCC"], "Test4": "TESTV4", "Test5": "OK"}5) $pushAll
5) $pushAll
Usage: {$pushAll: {Field:value_array}}
With $push, it is possible to append multiple values to an array field at a time. Cases:
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "count": +, "test1": ["AAA", "BBB", "CCC", ["ddd", "Eee"]], "test2": ["CCC"], "Test4": "TESTV4", "Test5": "OK"}
> db.test0.update ({"_id":}, {$pushAll: {"test1": ["fff", "GGG"]});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "count": +, "test1": ["AAA", "BBB", "CCC", ["ddd", "Eee"], "fff", "GGG"], "test2" : ["CCC"], "test4": "TESTV4", "Test5": "OK"}
6) $addToSet
Usage: {$addToSet: {Field:value}}
Adds a value to the array, and only if the value is not inside the array. Cases:
> db.test0.update ({"_id":}, {$addToSet: {"test1": {$each: ["444", "555"]}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "count": 18,
"Test1": ["AAA", "BBB", "CCC", ["ddd", "Eee"], "fff", "GGG", ["111", "222"], "444", "555"],
"Test2": ["CCC"], "test4": "TESTV4", "Test5": "OK"
}
> db.test0.update ({"_id":}, {$addToSet: {"test1": {$each: ["444", "555"]}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "count": 18,
"Test1": ["AAA", "BBB", "CCC", ["ddd", "Eee"], "fff", "GGG", ["111", "222"], "444", "555"], "test2": ["CCC"],
"Test4": "TESTV4", "Test5": "OK"
}
> db.test0.update ({"_id":}, {$addToSet: {"test1": ["444", "555"]}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "count": 18,
"Test1": ["AAA", "BBB", "CCC", ["ddd", "Eee"], "fff", "GGG", ["111", "222"], "444", "555", ["444", "555"]], "test2": ["CCC"],
"Test4": "TESTV4", "Test5": "OK"
}
> db.test0.update ({"_id":}, {$addToSet: {"test1": ["444", "555"]}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": {"}", "Count": +, "test1": ["AAA", "BBB", "CCC", ["ddd", "Eee"], "fff", "GGG", ["111", "222"], "444" , "555", ["444", "555"]], "test2": ["CCC"],
"Test4": "TESTV4", "Test5": "OK"
}
7) $pop
To delete a value within an array
Usage:
Delete the last value: {$pop: {field:1}} to delete the first value: {$pop: {field:-1}}
Note that only one value can be deleted, that is, only 1 or-1, not 2 or--and two are deleted. MongoDB 1.1 and later versions are only available, for example:
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "count": 18,
"Test1": ["BBB", "CCC", ["ddd", "Eee"], "fff", "GGG", ["111", "222"], "444"],
"Test2": ["CCC"], "test4": "TESTV4", "Test5": "OK"
}
> db.test0.update ({"_id":}, {$pop: {"Test1":-1}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "count": 18,
"Test1": ["CCC", ["ddd", "Eee"], "fff", "GGG", ["111", "222"], "444"],
"Test2": ["CCC"], "test4": "TESTV4", "Test5": "OK"
}
> db.test0.update ({"_id":}, {$pop: {"test1": 1}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "count": 18,
"Test1": ["CCC", ["ddd", "Eee"], "fff", "GGG", ["111", "222"]], "test2": ["CCC"], "test4": "TESTV4",
"Test5": "OK"
}
8) $pull
Usage: $pull: {Field:value}}
Deletes an equal value from the array field. Cases:
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": {"}", "Count": "Test1": ["CCC", ["ddd", "Eee"], "fff", "GGG", ["111", "222"], "TE St2 ": [" CCC "]," test4 ":" TESTV4 ",
"Test5": "OK"}
> db.test0.update ({"_id":}, {$pull: {"test1": "GGG"}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count": "Test1": ["CCC", ["ddd", "Eee"], "fff", ["111", "222"]], "test2": ["CCC"], "test4": "TESTV4", "Test5"
: "OK"}
9) $pullAll
Usage: {$pullAll: {Field:value_array}}
With $pull, you can delete multiple values within an array at a time. Cases:
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count": "Test1": ["CCC", ["ddd", "Eee"], "fff", ["111", "222"]], "test2": ["CCC"], "test4": "TESTV4", "Test5"
: "OK"}
> db.test0.update ({"_id":}, {$pullAll: {"test1": ["CCC", "FFF"]}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count": "Test1": [["ddd", "Eee"], ["111", "222"]], "test2": ["CCC"], "TE St4 ":" TESTV4 "," Test5 ":" OK "}
10) $ operator
$ is his own meaning, represented by the condition of finding an array inside an item of his own. Oh, compare the AU kou. Take a look at the official example:
> T.find ()
{"_id": ObjectId ("4b97e62bf1d8c7152c9ccb74"), "title": "ABC", "comments": [{"By": "Joe", "votes": 3}, {"By": "J Ane "," votes ": 7}]}
> t.update ({' comments.by ': ' Joe '}, {$inc: {' comments.$.votes ': 1}}, False, True)
> T.find ()
{"_id": ObjectId ("4b97e62bf1d8c7152c9ccb74"), "title": "ABC", "comments": [{"By": "Joe", "votes": 4}, {"By": "J Ane "," votes ": 7}]}
It is important to note that $ only applies the first array item found, followed by no matter. Or look at an example:
> T.find ();
{"_id": ObjectId ("4b9e4a1fc583fa1c76198319"), "X": [1, 2, 3, 2]}
> t.update ({x:2}, {$inc: {"x.$": 1}}, False, True);
> T.find ();
Also note that when used with $unset, a null array entry is left, but you can delete all null array items with {$pull: {x:null}}. Cases:
> T.insert ({x: [1,2,3,4,3,2,3,4]})
> T.find ()
{"_id": ObjectId ("4bde2ad3755d00000000710e"), "X": [1, 2, 3, 4, 3, 2, 3, 4]}
> t.update ({x:3}, {$unset: {"x.$": 1}})
> T.find ()
{"_id": ObjectId ("4bde2ad3755d00000000710e"), "X": [1, 2, NULL, 4, 3, 2, 3, 4]}
{"_id": ObjectId ("4b9e4a1fc583fa1c76198319"), "X": [1, 3, 3, 2]}
The update operator for MongoDB