Db. Collection name. Update ({Query},{update},upsert, multi})
Query: Filter conditions
Update: Modify Content
Upsert: If there are no records detected by the query criteria, whether to insert a data, the default is False
Multi: Whether to modify only the first record detected by the query condition, the default is False
> db.student.update ({_id:1}, {name: "Zhang"}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1 }) &G T Db.student.find () {"_id": 1, "name": "Zhang" } {"_id": 2, "name": "Lisi", "Age": } {"_id": 3, "name" : "Wangwu", "age": {"_id": 4, "name": "Zhaoliu", "age": {}} "_id": 5, "name": "Qianliu", "Age": 33< c7>} {"_id": 6, "name": "Sunba", "Age": +}
The entire data has been modified, if the modified content does not set the age field, then the previous age is also canceled, the data are all modified
Local modifications
$set: If there is a key to modify, add if no key exists
> db.student.update ({_id:1},{$set: {name: "Zhangsan", Age:26}}) Writeresult ({"nmatched": 1, "nupserted": 0, "NMo Dified ": 1 }" > db.student.find () {"_id": 1, "name": "Zhangsan", "age": + } {"_id": 2, "name": "Lisi" , "Age": } {"_id": 3, "name": "Wangwu", "age": {} {"_id": 4, "name": "Zhaoliu", "Age": } {"_id" : 5, "name": "Qianliu", "Age": ~ {"_id": 6, "name": "Sunba", "Age": +
Cannot be modified when ID is duplicated
> db.student.update ({_id:1},{_id:0, Name: "Zhangsanzhangsan"}) Writeresult ({"nmatched": 0, "nupserted": 0 , "nmodified": 0, "writeerror": {"code": 16837, "errmsg": "The _id field cannot is changed from {_id:1.0 } to {_id:0.0}. " }}) > db.student.find () {"_id": 1, "name": "Zhangsan", "age": + } {"_id": 2, "name": "Lisi", "Age": 2 7 } {"_id": 3, "name": "Wangwu", "Age": } {"_id": 4, "name": "Zhaoliu", "Age": } {"_id": 5, "NA Me ":" Qianliu "," Age ": ~ {" _id ": 6," name ":" Sunba "," Age ": +
The third parameter defaults to False, if no action is taken when the data is not found, true when a new data is inserted into the database
> db.student.update ({_id:7},{_id:7, Name: "Songjiu", Age:32}) Writeresult ({"nmatched": 0, "nupserted": 0, "Nmodi Fied ": 0 }" > Db.student.update ({_id:7},{_id:7, Name: "Songjiu"}, True) Writeresult ({"nmatched": 0, "Nupserte D ": 1," nmodified ": 0," _id ": 7 }) > db.student.find () {" _id ": 1," name ":" Zhangsan "," age ": + }" _i D ": 2," name ":" Lisi "," age ": {" _id ": 3," name ":" Wangwu "," age ": {} {" _id ": 4," name ":" Zhaoliu ", "Age": {"_id": 5, "name": "Qianliu", "Age": " {"} {"_id": 6, "name": "Sunba", "age": + } {"_id": 7, "name": "Songjiu", "Age":
The fourth parameter defaults to False,false, which means that only the first data that is queried is modified, and True indicates that all data is modified to the query
> db.student.update ({age:32}, {$set: {age:33}}, False, False) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) >Db.student.find () {"_id": 1, "name": "Zhangsan", "Age": 26} {"_id": 2, "name": "Lisi", "age": 27} {"_id": 3, "name": "Wangwu", "Age": 30} {"_id": 4, "name": "Zhaoliu", "Age": 28} {"_id": 5, "name": "Qianliu", "Age": 33} {"_id": 6, "name": "Sunba", "Age": 33} {"_id": 7, "name": "Songjiu", "Age": 32}> db.student.update ({age:33}, {$set: {age:32}}, False, True) Writeresult ({"nmatched": 2, "nupserted": 0, "nmodified": 2}) > Db.student.find () {"_id": 1, "name": "Zhangsan", "age": + } {"_id": 2, "name": "Lisi", "Age": } {"_id": 3, "name": "Wangwu", "Age": } {"_id": 4, "name": "Zhaoliu", "Age": } {"_id": 5, "name" : "Qianliu", "age": + } {"_id": 6, "name": "Sunba", "age": + } {"_id": 7, "name": "Songjiu", "Age": }> db.student.update ({age:32}, {$set: {age:33 }}) Writeresult ({"nmatched": 1, "nupserted": 0, " Nmodified ": 1 }); Db.student.find () {" _id ": 1," name ":" Zhangsan "," age ": + } {" _id ": 2," name " : "Lisi", "Age": } {"_id": 3, "name": "Wangwu", "age": + } {"_id": 4, "name": "Zhaoliu", "Age": 28 } {"_id": 5, "name": "Qianliu", "Age": } {"_id": 6, "name": "Sunba", "age": + } {"_id": 7, " Name ":" Songjiu "," age ": + /span>
$inc: How much to add on the original basis
> db.student.update ({_id:7}, {$inc: {age:-1}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1 }) & Gt Db.student.find () {"_id": 1, "name": "Zhangsan", "age": + } {"_id": 2, "name": "Lisi", "Age": } {"_ ID ": 3," "Name": "Wangwu", "age": {"_id": 4, "name": "Zhaoliu", "age": {}} "_id": 5, "name": "Qianli U "," Age ": {" _id ": 6," name ":" Sunba "," age ": + } {" _id ": 7," name ":" Songjiu "," Age ":-}
$unset: Remove a key
> db.student.update ({_id:7},{$unset: {age:1}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1 }) > db.student.find () {"_id": 1, "name": "Zhangsan", "age": + } {"_id": 2, "name": "Lisi", "age": + } {"_id": 3, "name": "Wangwu", "Age": } {"_id": 4, "name": "Zhaoliu", "Age": } {"_id": 5, "name": "Qi Anliu "," Age ": ~ {" _id ": 6," name ":" Sunba "," age ": + {" _id ": 7," name ":" Songjiu "}
Operations on Array types
$push: When a key does not exist in the data, the key for the array type is created, and the value is inserted, if the key is present, and the key is an array type, the data for this array type is appended, and if the key is present and the key is not an array type, an error is made.
$pushAll: bulk Append to Array
$addToSet: Does not append when there is a value in the array, append when there is no value
> db.student.update ({_id:7},{$push: {skill: "Java"}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) >Db.student.find () {"_id": 1, "name": "Zhangsan", "Age": 26} {"_id": 2, "name": "Lisi", "age": 27} {"_id": 3, "name": "Wangwu", "Age": 30} {"_id": 4, "name": "Zhaoliu", "Age": 28} {"_id": 5, "name": "Qianliu", "Age": 33} {"_id": 6, "name": "Sunba", "Age": 32} {"_id": 7, "name": "Songjiu", "Skill": ["Java"]}> db.student.update ({_id:7},{$push: {skill: "MongoDB"}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) >Db.student.find () {"_id": 1, "name": "Zhangsan", "Age": 26} {"_id": 2, "name": "Lisi", "age": 27} {"_id": 3, "name": "Wangwu", "Age": 30} {"_id": 4, "name": "Zhaoliu", "Age": 28} {"_id": 5, "name": "Qianliu", "Age": 33} {"_id": 6, "name": "Sunba", "Age": 32} {"_id": 7, "name": "Songjiu", "Skill": ["Java", "MongoDB"]}> db.student.update ({_id:7},{$push: {name: "111"}}) Writeresult ({"nmatched": 0, "nupserted": 0, "nmodified": 0, "Writeerror": {"Code": 16837, "errmsg": "The field ' name ' must be an array" is a type String in document {_id:7.0} "}}) > Db.student.update ({_id:7},{$pushAll: {skill:["JS", "C + +", "Java"]}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) >Db.student.find () {"_id": 1, "name": "Zhangsan", "Age": 26} {"_id": 2, "name": "Lisi", "age": 27} {"_id": 3, "name": "Wangwu", "age": {} {"_id": 4, "name": "Zhaoliu", "Age": } {"_id": 5, "name" : "Qianliu", "Age": ~ {"_id": 6, "name": "Sunba", "age": + {"_id": 7, "name": "Songjiu", "Skill": [ "Java", "MongoDB", "JS", "C + +", "Java" ]}> db.student.update ({_id:7},{$addToSet: {skill: "MongoDB"}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 0 }) > db.student.find () {"_id": 1, "name": "Zhan Gsan "," age ": + } {" _id ": 2," name ":" Lisi "," Age ": } {" _id ": 3," name ":" Wangwu "," age ": {}} _id ": 4," "Name": "Zhaoliu", "age": {"_id": 5, "name": "Qianliu", "age": = {"_id": 6, "name": "Su NBA "," Age ": + {" _id ": 7," name ":" Songjiu "," Skill ": [" Java "," MongoDB "," JS "," C + + "," Java "]}
$pop: Deletes the first or last element of an array with a value of 1 when the first element is deleted, and the value 1 o'clock is the last element to delete.
$pull: Deletes one of the specified values in the array
$pullAll: To delete multiple specified values in an array
> db.student.update ({_id:7},{$pop: {skill:1}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) >Db.student.find () {"_id": 1, "name": "Zhangsan", "Age": 26} {"_id": 2, "name": "Lisi", "age": 27} {"_id": 3, "name": "Wangwu", "Age": 30} {"_id": 4, "name": "Zhaoliu", "Age": 28} {"_id": 5, "name": "Qianliu", "Age": 33} {"_id": 6, "name": "Sunba", "Age": 32} {"_id": 7, "name": "Songjiu", "Skill": ["Java", "MongoDB", "JS", "C + +"]}> db.student.update ({_id:7},{$pop: {skill:-1}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) >Db.student.find () {"_id": 1, "name": "Zhangsan", "Age": 26} {"_id": 2, "name": "Lisi", "age": 27} {"_id": 3, "name": "Wangwu", "Age": 30} {"_id": 4, "name": "Zhaoliu", "Age": 28} {"_id": 5, "name": "Qianliu", "Age": 33} {"_id": 6, "name": "Sunba", "Age": 32} {"_id": 7, "name": "Songjiu", "Skill": ["MongoDB", "JS", "C + +"]}> db.student.update ({_id:7},{$pull: {skill: "JS"}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) >Db.student.find () {"_id": 1, "name": "Zhangsan", "Age": 26} {"_id": 2, "name": "Lisi", "age": 27} {"_id": 3, "name": "Wangwu", "Age": 30} {"_id": 4, "name": "Zhaoliu", "Age": 28} {"_id": 5, "name": "Qianliu", "Age": 33} {"_id": 6, "name": "Sunba", "Age": 32} {"_id": 7, "name": "Songjiu", "Skill": ["MongoDB", "C + +"]}> db.student.update ({_id:7},{$pullAll: {skill:["JS"]}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 0}) >Db.student.find () {"_id": 1, "name": "Zhangsan", "Age": 26} {"_id": 2, "name": "Lisi", "age": {"_id": 3, "name": "Wangwu", "Age": } {"_id": 4, "name": "Zhaoliu", "age": {"_id": 5, "name": "Qianliu", "Age": " {"}} "_id": 6, "name": "Sunba", "Age": } {"_id": 7, "name": "Songjiu", "Skill": ["MongoDB", "C + +" ]}> db.student.update ({_id:7},{$pullAll: {skill:["mo Ngodb "," C + + "]}) Writeresult ({" nmatched ": 1," nupserted ": 0," nmodified ": 1 }) > db.student.find () {" _id " : 1, "name": "Zhangsan", "age": + {"_id": 2, "name": "Lisi", "Age": } {"_id": 3, "name": "Wangwu", "Age": {"_id": 4, "name": "Zhaoliu", "age": {}} "_id": 5, "name": "Qianliu", "Age": " {}}" _ ID ": 6," "Name": "Sunba", "age": + {"_id": 7, "name": "Songjiu", "skill": []}
MongoDB Quick Start Learning Note 5 MongoDB document modification actions