Mongodb update There are two commands: update, save.
1.1update command
update command format:
db.collection.update(criteria,objNew,upsert,multi)
Parameter Description:
criteria: query
objNew: update objects and some of the newer operators
upsert: If there is no record update, insert objNew whether this new document, true to insert, default is false, not inserted.
multi: The default is false, only updating the first record found. If true, the query by the conditions out of all the records updated.
Example:
> db.classes.insert({"name":"c1","count":30}) > db.classes.insert({"name":"c2","count":30}) > db.classes.find() { "_id" : ObjectId("5030f3a3721e16c4ab180cd9"), "name" : "c1", "count" : 30 } { "_id" : ObjectId("5030f3ab721e16c4ab180cda"), "name" : "c2", "count" : 30 } >
Example 1: class name to modify count is greater than 20 c3
> db.classes.update({"count":{$gt:20}},{$set:{"name":"c3"}}) > db.classes.find() { "_id" : ObjectId("5030f3a3721e16c4ab180cd9"), "name" : "c3", "count" : 30 } { "_id" : ObjectId("5030f3ab721e16c4ab180cda"), "name" : "c2", "count" : 30 } >
Since the value and the multi upsert not specified, it defaults to all false, it can be seen from the results, only modify the records that meet the first condition.
Example 2: class name to modify count is greater than 20 c4, set to true multi
> db.classes.update({"count":{$gt:20}},{$set:{"name":"c4"}},false,true) > db.classes.find() { "_id" : ObjectId("5030f3a3721e16c4ab180cd9"), "name" : "c4", "count" : 30 } { "_id" : ObjectId("5030f3ab721e16c4ab180cda"), "name" : "c4", "count" : 30 } >
As specified by the multi it is true, so the two qualifying records have been updated.
Example 3: the class name of the count is greater than 50 modified to c5, set to true upsert
Do not count greater than 50 records in the collection, but as specified by the upsert is true, if it will not be found to insert a new record.
1.2save command
Mongodb another update command save, in the following format:
db.collection.save(obj)
Representative of an object obj to be updated, if the internal one and the same set of obj "_id" record already exists, the object obj Mongodb will replace the existing collection of records, if not present, will be inserted into the object obj.
This command is relatively simple, the example will be omitted.
Second, the data updating operator
1.$inc
Usage: {$ inc: {field: value}}
Role: a field for a numeric field increased value
Example: The name is chenzhou students of age increased by 5
> Db.students.find () { "the _id": the ObjectId ( "5030f7ac721e16c4ab180cdb"), "name": "Chenzhou", "Age":} 22 is # query results are displayed age 22 is > db.students.update ({name : "Chenzhou"}, {$ inc is: age {:}}. 5) # perform the modification, the age increases. 5 > db.students.find () { "the _id": the ObjectId ( "5030f7ac721e16c4ab180cdb"), "name": " Chenzhou "," Age ": 27} > # query results show the age of 27, successfully modified
2.$set
Usage: {$ set: {field: value}}
Role: to document the value of a field field to value
Example: The chenzhou age to 23 years old
> db.students.find() { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "name" : "chenzhou", "age" : 27 } > db.students.update({name:"chenzhou"},{$set:{age:23}}) > db.students.find() { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "name" : "chenzhou", "age" : 23 } >
You can see from the results, updated from the age of 27 became 23
3.$unset
Usage: {$ unset: {field: 1}}
Role: remove a field field
Example: chenzhou age field deleted
> db.students.find() { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "name" : "chenzhou", "age" : 23 } > db.students.update({name:"chenzhou"},{$unset:{age:1}}) > db.students.find() { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "name" : "chenzhou" } >
4.$push
Usage: {$ push: {field: value}}
Role: the value added in the field. Note: field can only be an array type, if the field does not exist, it will automatically insert an array type
Example: to add chenzhou alias "michael"
> db.students.find() { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "name" : "chenzhou" } > db.students.update({name:"chenzhou"},{$push:{"ailas":"Michael"}}) > db.students.find() { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "Michael" ], "name" : "chenzhou" } >
It can be seen from the results, recording additionally an array Alias type field, and a field for the "Michael" value
5kpushall
用法:{$pushAll:{field:value_array}}
Role: Use the same $ push the same, only $ pushAll append multiple values at once into an array field.
Example: to chenzhou additional alias A1, A2
> db.students.find() { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "Michael" ], "name" : "chenzhou" } > db.students.update({name:"chenzhou"},{$pushAll:{"ailas":["A1","A2"]}}) > db.students.find() { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "Michael", "A1", "A2" ], "name" : "chenzhou" } >
6.$addToSet
Usage: {$ addToSet: {field: value}}
Role: adding a value into the array, and only when there is no increase if the value in the array.
Example: To alias field chenzhou's add two aliases A3, A4
As can be seen from the results, updated ailas more than a field object that contains two data, namely, A3, A4
7.$pop
Usage: Delete the first array value: {$ pop: {field: -1}}, remove the last value within the array: {$ pop: {field: 1}}
Role: a value in the array is used to delete
Example: Remove chenzhou recorded in the first alias alias field
> db.students.find() { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "Michael", "A1", "A2", [ "A3", "A4" ] ], "name" : "chenzhou" } > db.students.update({name:"chenzhou"},{$pop:{"ailas":-1}}) > db.students.find() { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "A1", "A2", [ "A3", "A4" ] ], "name" : "chenzhou" } >
Can be read from the results, the first alias Michael has been deleted.
We then use the command to delete the last alias:
> db.students.find() { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "A1", "A2", [ "A3", "A4" ] ], "name" : "chenzhou" } > db.students.update({name:"chenzhou"},{$pop:{"ailas":1}}) > db.students.find() { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "A1", "A2" ], "name" : "chenzhou" } >
As can be seen from the results, alias field last alias [ "A3", "A4"] has been deleted.
8.$pull
Usage: {$ pull: {field: _value}}
Role: To delete a field from an array of equal value _value
Example: Remove chenzhou record alias A1
> db.students.find() { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "A1", "A2" ], "name" : "chenzhou" } > db.students.update({name:"chenzhou"},{$pull:{"ailas":"A1"}}) > db.students.find() { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ "A2" ], "name" : "chenzhou" } >
9.$pullAll
Usage: {$ pullAll: value_array}
Role: Use the same $ pull as multiple values in the array can be removed at once.
Example: To delete all the aliases in the record chenzhou
I can see A1 and A2 have all been deleted
10.$rename
用法:{$rename:{old_field_name:new_field_name}}
Role: The renaming fields
Example: The name field chenzhou recorded rename sname
> db.students.find() { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ ], "name" : "chenzhou" } > db.students.update({name:"chenzhou"},{$rename:{"name":"sname"}}) > db.students.find() { "_id" : ObjectId("5030f7ac721e16c4ab180cdb"), "ailas" : [ ], "sname" : "chenzhou" } >