MongoDB Data Update command

Source: Internet
Author: User
Tags aliases mongodb update

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
  1. > Db.classes.insert ({"name":"C1","Count": $)
  2. > Db.classes.insert ({"name":"C2","Count": $)
  3. > Db.classes.find ()
  4. { "_id": ObjectId ("5030f3a3721e16c4ab180cd9"), "name": "C1", "Count": $
  5. { "_id": ObjectId ("5030F3AB721E16C4AB180CDA"), "name": "C2", "Count": /c13>
  6. >

Example 1: Change the class name of count greater than 20 to C3

Shell Code
    1. > db.classes.update ({20}},{$set: {
    2. > db.classes.find ()   
    3. {  "5030F3A3721E16C4AB180CD9"),  30 }   
    4. {  "_id"  :  ObjectId ( "5030F3AB721E16C4AB180CDA"),   "C2",  30 }  
    5. >   

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
    1. > db.classes.update ({20}},{$set: {
    2. > db.classes.find ()   
    3. { " 5030F3A3721E16C4AB180CD9 "),  " name "  :  "C4",  30  }  
    4. {  "_id"  :  objectid ( "5030F3AB721E16C4AB180CDA"),   "C4",  30 }   
    5. >   

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
  1. > db.classes.update ({"Count": {$gt:50}},{$set: {"name":"C5"}},true,false)
  2. > Db.classes.find ()
  3. { "_id": ObjectId ("5030f3a3721e16c4ab180cd9"), "name": "C4", "Count": $
  4. { "_id": ObjectId ("5030F3AB721E16C4AB180CDA"), "name": "C4", "Count": $
  5. { "_id": ObjectId ("5030f589ce8fa8884e6cd441"), "name": "C5"}
  6. >

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
  1. > Db.students.find ()
  2. { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "name": "Chenzhou", "age":
  3. #查询结果显示年龄为
  4. > db.students.update ({name:"Chenzhou"},{$inc: {Age:5}})
  5. #执行修改, increase the age by 5 .
  6. > Db.students.find ()
  7. { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "name": "Chenzhou", "age": + }
  8. >
  9. #查询结果显示年龄为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
    1. > db.students.find ()   
    2. {  "_id"  : objectid ( "5030f7ac721e16c4ab180cdb"),   "name"  :  "Chenzhou",   "age"  :  27 }  
    3. >  Db.students.update ({name: "Chenzhou"},{$set: {age:23}})   
    4. > db.students.find ()   
    5. {  "_id"  : objectid ( "5030f7ac721e16c4ab180cdb"),   "Chenzhou",  23  }  
    6. >   

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
    1. > Db.students.find ()
    2. { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "name": "Chenzhou", "age": $
    3. > db.students.update ({name:"Chenzhou"},{$unset: {Age:1}})
    4. > Db.students.find ()
    5. { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "name": "Chenzhou"}
    6. >

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
    1. > db.students.find ()   
    2. {  "_id"  : objectid ( "5030f7ac721e16c4ab180cdb"),  
    3. > db.students.update ({name: "Michael"})   
    4. > db.students.find ( )   
    5. {  " 5030f7ac721e16c4ab180cdb "),  " Ailas " : [ " Michael " ], " name " : " Chenzhou " }  
    6. >   

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
  1. > Db.students.find ()
  2. { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "Ailas": [ "Michael"], "name": "Chenzhou"}
  3. > db.students.update ({name:"Chenzhou"},{$pushAll: {"Ailas": ["A1","A2"]}})
  4. > Db.students.find ()
  5. { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "Ailas": [ "Michael", "A1", "A2"], "name": "Chenzhou"}
  6. >

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
  1. > Db.students.find ()
  2. { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "Ailas": [ "Michael", "A1", "A2"], "name": "Chenzhou"}
  3. > db.students.update ({name:"Chenzhou"},{$addToSet: {"Ailas": ["A3","A4"]}})
  4. > Db.students.find ()
  5. { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "Ailas": [ "Michael", "A1", "A2", [ "A3", "A4"], "name": "Chenzhou"}
  6. >

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
  1. > Db.students.find ()
  2. { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "Ailas": [ "Michael", "A1", "A2", [ "A3", "A4"], "name": "Chenzhou"}
  3. > db.students.update ({name:"Chenzhou"},{$pop: {"Ailas":-1}})
  4. > Db.students.find ()
  5. { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "Ailas": [ "A1", "A2", [ "A3", "A4"]], "Name": "Chenzhou"}
  6. >

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
  1. > Db.students.find ()
  2. { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "Ailas": [ "A1", "A2", [ "A3", "A4"]], "Name": "Chenzhou"}
  3. > db.students.update ({name:"Chenzhou"},{$pop: {"Ailas":1}})
  4. > Db.students.find ()
  5. { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "Ailas": [ "A1", "A2"], "name": "Chenzhou" }  
  6. >

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
  1. > Db.students.find ()
  2. { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "Ailas": [ "A1", "A2"], "name": "Chenzhou" }  
  3. > db.students.update ({name:"Chenzhou"},{$pull: {"Ailas":"A1"}})
  4. > Db.students.find ()
  5. { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "Ailas": [ "A2"], "name": "Chenzhou"} /c15>
  6. >

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
  1. > Db.students.find ()
  2. { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "Ailas": [ "A1", "A2"], "name": "Chenzhou" }  
  3. > db.students.update ({name:"Chenzhou"},{$pullAll: {"Ailas": ["A1","A2"]}})
  4. > Db.students.find ()
  5. { "_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "Ailas": [], "name": "Chenzhou"} /c1>
  6. >

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
    1. > db.students.find ()   
    2. {  "_id"  : objectid ( "5030f7ac721e16c4ab180cdb"),   "Ailas"  : [ ],  "name"  :  "Chenzhou"  }  
    3. >  Db.students.update ({name: "Chenzhou"},{$rename: { "name": "Sname"}})   
    4. > db.students.find ()   
    5. { " _id " : objectid (" 5030f7ac721e16c4ab180cdb "),  "Ailas"  : [ ],  "sname"  :  "Chenzhou"  }  
    6. >    

As a result, you can see that the Name field has been updated to sname.

MongoDB Data Update command

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.