Mongodb
MongoDB is an Open-source document database, and a leading NoSQL database. MongoDB is written by the C + + language.
Document
A document is a set of key-value pairs. File dynamic mode. Dynamic mode means that documents in the same collection do not need a collection of common fields of the same field or structure group to hold different types of data.
Db. Collection name. Update ({Query},{update},upsert, multi})
Query: Filter conditions
Update: Modifying content
Upsert: If there are no records detected by query criteria, insert a piece of 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})
> 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}
The whole piece of data has been modified, and if the age field is not set in the modified content, the previous age has been canceled, and the data has been modified
Local modifications
$set: If there are keys to modify, if there is no key to add
> db.student.update ({_id:1},{$set: {name: "Zhangsan", age:26})
Writeresult ({"nmatched": 1, "nupserted": 0, "NM Odified ": 1}"
> Db.student.find ()
{"_id": 1, "name": "Zhangsan", "age":
{"_id": 2, "name": "L ISI "," Age ":"
{"_id": 3, "name": "Wangwu", "age":
{"_id": 4, "name": "Zhaoliu", "Age":
{"_id": 5, "name": "Qianliu", "Age": 33}
Cannot be modified if ID repeats
> db.student.update ({_id:1},{_id:0, Name: "Zhangsanzhangsan"})
Writeresult ({
"nmatched": 0,
" Nupserted ": 0,
" nmodified ": 0,
" writeerror ": {
" code ": 16837,
" errmsg ":" The _id field cannot be Chan Ged from {_id:1.0} to {_id:0.0}}
)
> Db.student.find ()
{"_id": 1, ' name ': ' Zhangsan ', ' age ': '
{' _id ': 2, ' name ': ' Lisi ', ' age ': 27}
{"_id": 3, "name": "Wangwu", "Age":
{"_id": 4, "name": "Zhaoliu", "Age":}
{"_id": 5, "name": "Qianliu", "Age": 33}
The third argument defaults to False, and if no action is performed when data false is not found, true to insert a new data into the database
> db.student.update ({_id:7},{_id:7, Name: "Songjiu", age:32})
Writeresult ({"nmatched": 0, "nupserted": 0, "Nmod Ified ": 0}"
> Db.student.update ({_id:7},{_id:7, Name: "Songjiu"}, True)
Writeresult ({"nmatched": 0, "Nupse RTed ": 1," nmodified ": 0," _id ": 7})
> Db.student.find ()
{" _id ": 1," name ":" Zhangsan "," Age ":}
{"_id": 2, "name": "Lisi", "Age": M}
{"_id": 3, "name": "Wangwu", "age":
{"_id": 4, "name": "En Aoliu ', ' age ':
{' _id ': 5, ' name ': ' Qianliu ', ' age ': '
_id ': 6, ' name ': ' Sunba ', ' Age ': 32}
The fourth parameter, when default is False,false, represents modifying only the first piece of data that is queried, and true indicates that all the data that is queried is modified
> 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": {"_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": > 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": 26}
{"_id": 2, "name": "Lisi", "Age": 27}
{"_id": 3, "name": "Wangwu", "Age": 30}
{"_id": 4, "name": "Zhaoliu", "Age": 28}
{"_id": 5, "name": "Qianliu", "Age": 32}
{"_id": 6, "name": "Sunba", "Age": 32} {"_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": 2 6} {"_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, ' NA Me ":" Songjiu "," Age ": 32}
$inc: How much is added on the original basis
> db.student.update ({_id:7}, {$inc: {age:-1}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.student.find ()
{"_id": 1, "name": "Zhangsan", "age":
{"_id": 2, "name": "Lisi", "age": 27}< c5/>{"_id": 3, "name": "Wangwu", "age":
{"_id": 4, "name": "Zhaoliu", "age":
{"_id": 5, "name" : "Qianliu", "Age": 32}
{"_id": 6, "name": "Sunba"
$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": 27}
{"_id": 3, "name": "Wangwu", "Age":
{"_id": 4, "name": "Zhaoliu", "Age":}
{"_id": 5, "name": "Qianliu", "Age":
{"_id": 6, "name": "Sunba", "Age": 32}
Operations on Array types
$push: Creates the key of the array type and inserts the value when the key is not present in the data, appends the data of the array type if the key exists, and the key is an array type, and the error occurs if the key is present and is not an array type.
$pushAll: Appending to array in bulk
$addToSet: No append when the value is 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": {"_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": ["java"]} > Db.student . Update ({_id:7},{$push: {skill: MongoDB "}}) Writeresult ({" nmatched ": 1," nupserted ": 0," nmodified ": 1}) > Db.stud Ent.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 ": [" Java "," MongoDB "]} > Db.student.u Pdate ({_id:7},{$push: {nAme: "Writeresult"}}) ({"nmatched": 0, "nupserted": 0, "nmodified": 0, "Writeerror": {"code": 16837, "errmsg": " The field ' name ' must be a array but IS's type String in document {_id:7.0} '}}) > Db.student.update ({_id:7},{$pus hall:{skill:["JS", "C + +", "Java"}}) 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 ': 6, ' n Ame ":" 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": "Zhangsan", "Age":} {"_id": 2, "name": "Lisi", "Age": "_id" : 3, "name": "Wangwu",' Age ': {' _id ': 4, ' name ': ' Zhaoliu ', ' age ': ' {' _id ': 5, ' name ': ' Qianliu ', ' age ': ' _id ': 6, ' Nam E ":" Sunba "," Age ": {} {" _id ": 7," name ":" Songjiu "," Skill ": [" Java "," MongoDB "," JS "," C + + "," Java "]}
$pop: Deletes the first or last element of the array, with a value of-1 to delete the first element, and a value of 1 o'clock to delete the last element.
$pull: Deletes one of the specified values in the array
$PULLALL: Deletes multiple specified values in an array
> db.student.update ({_id:7},{$pop: {skill:1}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) > D B.student.find () {"_id": 1, "name": "Zhangsan", "Age":} {"_id": 2, "name": "Lisi", "age": "{_id": 3, "n
Ame ': ' Wangwu ', ' age ': {' _id ': 4, ' name ': ' Zhaoliu ', ' age ': ' {' _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: {Skil L:-1}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) > Db.student.find () {"_id": 1, "name": "En Angsan ', ' 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 ': 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 ': ' {' _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": ["MongoDB", "C + +"]} > Db.student.update ({_id:7},{$pull all:{skill:["JS"}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 0}) > Db.student.find () {"_id": 1,
' Name ': ' Zhangsan ', ' age ': {' _id ': 2, ' name ': ' Lisi ', ' age ': ' {' _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:[" Mongod B ", C + +"]}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) > Db.student.find () {"_id": 1, "name": "Zhangs An ', ' age ': {' _id ': 2, ' name ': ' Lisi ', ' age ': ' {' _id ': 3, ' name ': ' Wangwu ', ' age ': ' {' _id ': 4, ' NA Me ': ' Zhaoliu ', ' age ': {' _id ': 5, ' name ': ' Qianliu ', ' Age ': 6} {' _id ': + ' ' name ': ' Sunba ', ' Age ': 32} { "_id": 7, "name": "Songjiu", "skill": []}