MongoDB data updates A subdocument that specifies criteria

Source: Internet
Author: User
Tags aliases function prototype mongodb openid

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, insert objnew This new document, True is insert, default is False, do not insert.

Multi: Default is False, only the first record found is updated. True to update all records that are queried by condition.

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":}  
6.>  
> 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":
> Example 1: Modify class name with count greater than 20 to C3

Shell Code
1.> db.classes.update ({"Count": {$gt: 20}},{$set: {"name": "C3"}})
2.> Db.classes.find ()
3.{"_id": ObjectId ("5030F3A3721E16C4AB180CD9"), "name": "C3", "Count": 30}
4.{"_id": ObjectId ("5030F3AB721E16C4AB180CDA"), "name": "C2", "Count": 30}
5.>
> 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}
> Because the value of Upsert and multi is not specified, all defaults to false, and the result shows that only the first qualifying record has been modified.

Example 2: Change the class name of count greater than 20 to C4 and set multi to True

Shell Code
1.> db.classes.update ({"Count": {$gt: 20}},{$set: {"name": "C4"}},false,true)
2.> Db.classes.find ()
3.{"_id": ObjectId ("5030F3A3721E16C4AB180CD9"), "name": "C4", "Count": 30}
4.{"_id": ObjectId ("5030F3AB721E16C4AB180CDA"), "name": "C4", "Count": 30}
5.>
> 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}
> The two qualifying records were updated because multi was specified as true.

Example 3: Change the class name of count greater than 50 to C5 and 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": 30}
4.{"_id": ObjectId ("5030F3AB721E16C4AB180CDA"), "name": "C4", "Count": 30}
5.{"_id": ObjectId ("5030f589ce8fa8884e6cd441"), "name": "C5"}
6.>
> db.classes.update ({"Count": {$gt: 50}},{$set: {"name": "C5"}},true,false)
> Db.classes.find ()
{"_id": ObjectId ("5030F3A3721E16C4AB180CD9"), "name": "C4", "Count": 30}
{"_id": ObjectId ("5030F3AB721E16C4AB180CDA"), "name": "C4", "Count": 30}
{"_id": ObjectId ("5030f589ce8fa8884e6cd441"), "name": "C5"}
> There are no records with count greater than 50 in the collection, but a new record will be inserted if it is not found because Upsert is specified as true.

1.2save command

MongoDB Another update command is save, in the following format:

Db.collection.save (obj)

Obj represents the object that needs to be updated, and if a record with the same "_id" as obj already exists within the collection, MongoDB replaces the Obj object with a record that already exists within the collection, and if it does not, inserts the Obj object.

This command is simpler and the example is omitted.

II. Data Update operators

1. $inc

Usage: {$inc: {Field:value}}

Add value to a field in a number field

Example: Increase the age of a student with name Chenzhou by 5

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

Usage: {$set: {Field:value}}

Function: Set the value of a field in the document to

Example: Set the age of Chenzhou to 23 years old

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"), "name": "Chenzhou", "Age": 23}
6.>
> 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}
> from the results can be seen, after the update age from 27 into 23

3. $unset

Usage: {$unset: {field:1}}

Function: Delete a field

Example: Remove the Chenzhou age field

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

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: Add alias "Michael" to Chenzhou

Shell Code
1.> Db.students.find ()
2.{"_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "name": "Chenzhou"}
3.> db.students.update ({name: "Chenzhou"},{$push: {"Ailas": "Michael"}})
4.> Db.students.find ()
5.{"_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "Ailas": ["Michael"], "name": "Chenzhou"}
6.>
> 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"}
> From the result you can see that 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}}

Role: Use the same as $push, only $pushall can append multiple values to an array field at once.

Example: Append 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.>
> 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}}

Action: Adds a value to the array and increases only if the value does not exist in the array.

Example: Add two aliases to Chenzhou's name 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.>
> 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"}
> from the results can be seen, updated Ailas field after an object, this object contains 2 data, respectively, A3, A4

7. $pop

Usage: Delete the first value within the array: {$pop: {field:-1}}, delete the last value within the array: {$pop: {field:1}}

Function: To delete a value within an array

Example: Deletes 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.>
> 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"}
> The result can be read, the first alias Michael has been removed.

We then use the command to remove 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.>
> 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 you can see from the result, the last alias in the alias field ["A3", "A4"] is deleted.

8. $pull

Usage: {$pull: {Field:_value}}

Function: Deletes a value equal to _value from the array field

Example: Deleting 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"}
6.>
> 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 as $pull, you can delete multiple values within the array.

Example: Delete all aliases in the 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"}
6.>
> 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"}
> can see that A1 and A2 have all been removed.

$rename

Usage: {$rename: {old_field_name:new_field_name}}

Role: Rename a field

Example: Rename the Chenzhou Record's name field 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.>
> 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"}
> From the results you can see that the Name field has been updated to sname.

Have such a document

{
    "_id": ObjectId ("5689db252d162c9881532986"),
    "OpenID": "Xialei",
    "channels": [
        {
             "channel_id": "C1"
       },
        {
             "channel_id": "C2"
       },
         {
            "channel_id": "C2"
       },
        {
             "channel_id": "C2"
        }
   ]

The requirement now is to remove C2 from the channels of the document, but the master record should be retained.

You cannot use the Remove method at this time, this method will delete the entire document, query the official document found that there is a findandmodify method

The function prototype is as follows

Db.collection.findAndModify ({
Query: <document>,
Sort: <document>
Remove: <boolean>
Update: <DOCUMENT>
NEW: <boolean>
Fields: <document>
Upsert: <boolean>
Bypassdocumentvalidation: <boolean>
});
According to gourd painting scoop, pass the parameters in the

Db.collection.findAndModify ({
Query: {"OpenID": "Xialei"},
Update: {"$pull": {"channels": {"channel_id": "C2"}}},
New:true
});
After the update is

{
    "_id": ObjectId ("5689db252d162c9881532986"),
    "OpenID": "Xialei",
    "channels": [
        {
    & nbsp;       "channel_id": "C1"
       }
   ]
}
$pull is an array operator, understand the operation in update, I believe you can extrapolate, such as to insert a document will certainly think of using $push.

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.