Continue to learn MongoDB, the main content of this study is to use the modifier to complete the local update operation
1. $set
Used to specify a key-value pair, which is modified if there is a key, and is added if it does not exist
Db.person.update ({"Name": "Xiaoming"},{$set: {"Age": 27}}) If the age key is present, the value of the age key is changed to 27, and if it does not exist, the age key is added to the document with a value of 27
2. $inc
Can only be used with numeric types, which can add the numeric value of a specified key to a specified size.
Db.person.update ({"Age": 27},{$inc: {"1}}") After this operation, the 1 is added
3. $unset
Deletes the specified key
Db.person.update ({"Age": 27},{$unset: {' Age ': 1}})
4. $push
-If the specified key is an array, the new value is added.
-If the specified key is not an array, the current operation is interrupted.
-Creates a key value pair for an array type if the specified key does not exist
Db.person.update ({"Name": "Xiaoming"},{$push: {books: "中文版"})
If books this array exists, it will be added to the new value 中文版
If books does not exist, the books array is created and the value is added to Chinese
If the books key already exists and is not an array, an error will be
5. $pushAll
You can add data to the array in batches
Db.person.uipdate ({"Name": "Xiaoming"},{$pushAll: ["Math", "Chinese", "PE"]})
6.addToSet
If the target array exists, it is not operational and does not exist, then add this entry
Db.person.update ({"Name": "Xiaoming"},{$addToSet: {books: "Math"}})
7. $pop
Removes a value from the specified array, 1 deletes the last value, 1 deletes the first value
Db.person.update ({"Name": "Xiaoming"},{$pop: {books:-1}})
8. $pull
Deletes a specified value
Db.person.update ({"Name": "Xiaoming"},{$pull: {books: "中文版"}})
9. $pullAll
Delete multiple specified values at once
Db.person.update ({"Name": "Xiaoming"},{$pullAll: {books:["中文版", "Math"]}})
10.$
Array locator, if the array has multiple values, we only want to operate on one part of it, we need to use the locator $
For example:
If there is a document {"name": "Xiaoming", books:[{"type": "JS", "name": "EXTJS4"},{"type": "JS", "name": "JQUERY"}},{"type": "DB", "name ":" MongoDB "}}]}
Want to add a document of type equal to JS to the same author author is Xiaohua
Db.person.update ({"Books.type": "JS"},{$set: {"Books.$.author": "Xiaohua"}})
Remember: The modifier is placed on the outermost, the following reference to the query is placed in the inner layer!
MongoDB Learning Notes < three >