One, mongodb Data Update command
MongoDB Update has two commands: Update, save.
1.1update command
UPDATE command format:
Db.collection.update (Criteria,objnew,upsert,multi)
Parameter description:
Criteria: Query criteria
Objnew:update objects and some update operators
Upsert: If there is no record of update, whether to insert objnew this new document, true to insert, default to False, not to insert.
Multi: The default is false, updating only the first record found. If true, all records that are queried by criteria are updated.
Example:
Shell Code
- > Db.classes.insert ({"name":"C1","Count": $)
- > Db.classes.insert ({"name":"C2","Count": $)
- > Db.classes.find ()
- { "_id": ObjectId ("5030f3a3721e16c4ab180cd9"), "name": "C1", "Count": $
- { "_id": ObjectId ("5030F3AB721E16C4AB180CDA"), "name": "C2", "Count": /c13>
- >
Example 1: Change the class name of count greater than 20 to C3
Shell Code
- > db.classes.update ({20}},{$set: {
- > db.classes.find ()
- { "5030F3A3721E16C4AB180CD9"), 30 }
- { "_id" : ObjectId ( "5030F3AB721E16C4AB180CDA"), "C2", 30 }
- >
Because the values of Upsert and multi are not specified, all defaults to false, as the result shows, only the first record that meets the criteria is modified.
Example 2: Change the class name of count greater than 20 to C4, set multi to True
Shell Code
- > db.classes.update ({20}},{$set: {
- > db.classes.find ()
- { " 5030F3A3721E16C4AB180CD9 "), " name " : "C4", 30 }
- { "_id"  : objectid ( "5030F3AB721E16C4AB180CDA"), "C4", 30 }
- >
Because multi is specified as true, two records that match the criteria are updated.
Example 3: Change the class name of count greater than 50 to C5, set Upsert to True
Shell Code
- > db.classes.update ({"Count": {$gt:50}},{$set: {"name":"C5"}},true,false)
- > Db.classes.find ()
- { "_id": ObjectId ("5030f3a3721e16c4ab180cd9"), "name": "C4", "Count": $
- { "_id": ObjectId ("5030F3AB721E16C4AB180CDA"), "name": "C4", "Count": $
- { "_id": ObjectId ("5030f589ce8fa8884e6cd441"), "name": "C5"}
- >
There are no records in the collection that count is greater than 50, but because Upsert is specified as true, a new record is inserted if it is not found.
1.2save command
MongoDB Another update command is save, in the following format:
Db.collection.save (obj)
Obj represents an object that needs to be updated, and MongoDB replaces the Obj object with a record that already exists in the collection if there is already a record of "_id" that is identical to obj, and inserts the Obj object if it does not exist.
This command is relatively simple and the example is omitted.
Second, data update operator
1. $inc
Usage: {$inc: {Field:value}}
Function: Adds value to a field of a number
Example: Increase the age of the student with name Chenzhou by 5
Shell Code
- > Db.students.find ()
- { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "name": "Chenzhou", "age":
- #查询结果显示年龄为
- > db.students.update ({name:"Chenzhou"},{$inc: {Age:5}})
- #执行修改, increase the age by 5 .
- > Db.students.find ()
- { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "name": "Chenzhou", "age": + }
- >
- #查询结果显示年龄为27, modified successfully
2. $set
Usage: {$set: {Field:value}}
Function: Sets the value of a field in a document
Example: Set the age of Chenzhou to 23 years
Shell Code
- > db.students.find ()
- { "_id" : objectid ( "5030f7ac721e16c4ab180cdb"), "name" : "Chenzhou", "age"  : 27 }  
- > Db.students.update ({name: "Chenzhou"},{$set: {age:23}})
- > db.students.find ()
- { "_id" : objectid ( "5030f7ac721e16c4ab180cdb"), "Chenzhou", 23 }
- >
As you can see from the results, the age of the update changed from 27 to 23.
3. $unset
Usage: {$unset: {field:1}}
Role: Delete a field
Example: Remove the Age field for Chenzhou
Shell Code
- > Db.students.find ()
- { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "name": "Chenzhou", "age": $
- > db.students.update ({name:"Chenzhou"},{$unset: {Age:1}})
- > Db.students.find ()
- { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "name": "Chenzhou"}
- >
4. $push
Usage: {$push: {Field:value}}
Function: Append value to field. Note: field can only be an array type, and if field does not exist, an array type is automatically inserted
Example: Adding an alias "Michael" to Chenzhou
Shell Code
- > db.students.find ()
- { "_id" : objectid ( "5030f7ac721e16c4ab180cdb"),
- > db.students.update ({name: "Michael"})
- > db.students.find ( )
- { " 5030f7ac721e16c4ab180cdb "), " Ailas " : [ " Michael " ], " name " : " Chenzhou " }
- >
As you can see from the results, an array type field alias is appended to the record, and the field has a value of "Michael"
5.pushAll
Usage: {$pushAll: {Field:value_array}}
Function: Use the same as $push, except that $pushall can append multiple values to an array field at a time.
Example: Appending an alias to Chenzhou A1,A2
Shell Code
- > 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}}
Function: Adds a value to the array, and only increases if the value does not exist in the array.
Example: Add two aliases to the Chenzhou name field A3, A4
Shell Code
- > Db.students.find ()
- { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "Ailas": [ "Michael", "A1", "A2"], "name": "Chenzhou"}
- > db.students.update ({name:"Chenzhou"},{$addToSet: {"Ailas": ["A3","A4"]}})
- > Db.students.find ()
- { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "Ailas": [ "Michael", "A1", "A2", [ "A3", "A4"], "name": "Chenzhou"}
- >
As can be seen from the results, after the update Ailas field more than one object, this object contains 2 data, respectively, A3, A4
7. $pop
Usage: Delete the first value in the array: {$pop: {field:-1}}, delete the last value in the array: {$pop: {field:1}}
Function: Used to delete a value within an array
Example: Delete the first alias in the Alias field in a Chenzhou record
Shell Code
- > 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"}
- >
By the result can read a book, the first alias Michael has been deleted.
We then use the command to delete the last alias:
Shell Code
- > 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, the last alias ["A3", "A4"] in the alias field was deleted.
8. $pull
Usage: {$pull: {Field:_value}}
Function: Removes a value equal to _value from the array field
Example: Delete an alias in a Chenzhou record A1
Shell Code
- > 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"} /c15>
- >
9. $pullAll
Usage: {$pullAll: Value_array}
Function: Use the same as $pull, you can delete multiple values in an array at once.
Example: Delete all aliases within a Chenzhou record
Shell Code
- > Db.students.find ()
- { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "Ailas": [ "A1", "A2"], "name": "Chenzhou" }
- > db.students.update ({name:"Chenzhou"},{$pullAll: {"Ailas": ["A1","A2"]}})
- > Db.students.find ()
- { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "Ailas": [], "name": "Chenzhou"} /c1>
- >
You can see that A1 and A2 have all been removed.
Ten. $rename
Usage: {$rename: {old_field_name:new_field_name}}
Role: Renaming a field
Example: Rename the Name field of the Chenzhou record to sname
Shell Code
- > 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"  }  
- >
As a result, you can see that the Name field has been updated to sname.
MongoDB Data Update command