1, update () command basic use, (by default, just update a query to meet the criteria of the information does not exist by default, not added)
Note that the simple use of Db.user.update ({"Name": "User6"},{name: "user111"},0,1) will replace records that meet the query criteria {"Namne": "User6"} with a new field information and should use the Set, $inc and other update records.
db.collection.update (criteria, objnew, Upsert, Multi) // criteria:update Query conditions, similar to those in the SQL update query // objnew : Update object and some updated operators (such as $, $inc ... ), and so on, can also be understood as the following in the SQL update query ///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, you can find out all the records updated by criteria
Case:
Db.test0.update ({"Count": {$gt: 1}}, {$set: {"test2": "OK"}});//only the first record is updatedDb.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);//it's all updated.Db.test0.update ({"Count": {$gt: Ten}}, {$inc: {"Count": 1}},false,false);//only updated the first
$inc Use {$inc: {"name": xx}} xx is an integer indicating that the increment xx is a negative number and the field is added when the field does not exist.
>Db.user.find () {"_id": ObjectId ("54feead226be41dca9db0d24"), "name": "Wangping", "age": +, "sex": "Woman", "location": {"City": "B Eijing "," Road ":" Zhongguonongyedaxue " } }{ "_id": ObjectId ("54feef3626be41dca9db0d25"), "name": "Gaofei", "Age": 5, "Grade": [1, 2,, 6], "location": {"pro Vince ":" Shandong "," City ":" Jiyang "," Road ":" Zuozhuangzhen " } }> db.user.update ({},{$inc: {"age": 10}},0,1)//age all increases by 10 yearsWriteresult ({"nmatched": 2, "nupserted": 0, "nmodified": 2 })>Db.user.find () {"_id": ObjectId ("54feead226be41dca9db0d24"), "name": "Wangping", "age": +, "sex": "Woman", "location": {"City": "B Eijing "," Road ":" Zhongguonongyedaxue " } }{ "_id": ObjectId ("54feef3626be41dca9db0d25"), "name": "Gaofei", "Age": The "Grade": [1, 2, 5, 6], "location": {"pro Vince ":" Shandong "," City ":" Jiyang "," Road ":" Zuozhuangzhen " } }>
2, $unset use means to delete a field.
Db.user.update ({"Name": "Zhaoxingzhuang"},{$unset: {age:1},0,1})//Indicates that the age field of the qualifying data is deleted.
3, $push {$push: {arr:4}} $pushall {$pushAll: {arr:[4,6,7]}} if the field does not exist, the array data is added only for array operations
>Db.user.find () {"_id": ObjectId ("54feead226be41dca9db0d24"), "name": "Wangping", "age": +, "sex": "Woman", "location": {"City": "B Eijing "," Road ":" Zhongguonongyedaxue " } }{ "_id": ObjectId ("54feef3626be41dca9db0d25"), "name": "Gaofei", "Age": The "Grade": [1, 2, 5, 6], "location": {"pro Vince ":" Shandong "," City ":" Jiyang "," Road ":" Zuozhuangzhen " } }> db.user.update ({},{$push: {grade:4}},0,1)//Adding a 4 field to the grade array does not exist and will also be addedWriteresult ({"nmatched": 2, "nupserted": 0, "nmodified": 2 })>Db.user.find () {"_id": ObjectId ("54feead226be41dca9db0d24"), "name": "Wangping", "age": +, "sex": "Woman", "location": {"City": "B Eijing "," Road ":" Zhongguonongyedaxue "}," Grade ": [4 ] }{ "_id": ObjectId ("54feef3626be41dca9db0d25"), "name": "Gaofei", "Age": The "Grade": [1, 2, 5, 6, 4], "location": {" Province ":" Shandong "," City ":" Jiyang "," Road ":" Zuozhuangzhen " } }>
4, $pop Delete the values in the array db.a.update ({"Name": "Zhuang"},{$pop: {arr:1}}) Represents delete from front arr:-1 represents from behind , cannot delete more than one note the order of the back and forth here can understand that arrays are implemented in the form of a stack
> Db.user.find ({name: "Gaofei""_id": ObjectId ("54feef3626be41dca9db0d25"), "name": "Gaofei", "age": 34, " Grade ": [1, 2, 5, 6, 4]," location ": {" province ":" Shandong "," City ":" Jiyang "," Road ":" Zuozhuangzhen " }}> db.user.update ({"Name": "Gaofei"},{$pop: {grade:1"nmatched": 1, "nupserted": 0, "nmodified": 1 })
> Db.user.find ({name: "Gaofei""_id": ObjectId ("54feef3626be41dca9db0d25"), "name": "Gaofei", "Age": 34 , "Grade": [1, 2, 5, 6], "location": {"province": "Shandong", "City": "Jiyang"
5. $addToSet
MongoDB Update () operation