MongoDB Data Update command, operator

Source: Internet
Author: User
Tags mongodb 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    > db.classes.insert ({"Name": "C1", "Count": ())    >  Db.classes.insert ({"Name": "C2", "Count": $)    > db.classes.find ()    {  "_id"  : objectid ("5030f3a3721e16c4ab180cd9"),  "name"  :  "C1",  "Count"  : 30  }   {  "_id"  : objectid ("5030F3AB721E16C4AB180CDA"),  "name"  :  "C2" ,  "Count"  : 30 }   >   

Example 1: Modify the class name of count greater than 20 to C3 shell Code > db.classes.update ({"Count": {$gt: 20}},{$set: {"name": "C3"}}) > Db.classes.find () {"_id": ObjectId ("5030F3A3721E16C4AB180CD9"), "name": "C3", "Count": $ {"_id": ObjectId ("50 30F3AB721E16C4AB180CDA ")," name ":" C2 "," Count ": >

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 ({"Count": {$gt: 20}},{$set: {"name": "C4"}} , false,true) > Db.classes.find () {"_id": ObjectId ("5030F3A3721E16C4AB180CD9"), "name": "C4", "Count": 30} { "_id": ObjectId ("5030F3AB721E16C4AB180CDA"), "name": "C4", "Count": >

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": 30} { "_id": ObjectId ("5030F3AB721E16C4AB180CDA"), "name": "C4", "Count": $ {"_id": ObjectId ("5030f589ce8fa8884e6cd44 1 ")," 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: Add the age of the student named Chenzhou 5 Shell Code > db.students.find () {"_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "name": " Chenzhou "," Age ": #查询结果显示年龄为22 > Db.students.update ({name:" Chenzhou "},{$inc: {age:5}}) #执行修改, increase the age by 5 > Db.students.find () {"_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "name": "Chenzhou", "Age": > #查询结果显示年龄 Is 27, the modification succeeds

2. $set

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

Function: Sets the value of a field in a document

Example: Set the age of Chenzhou to 23-year-old shell code > db.students.find () {"_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "name": "Chenzh Ou "," Age ": $ > Db.students.update ({name:" Chenzhou "},{$set: {age:23}}) > Db.students.find () {" _id ": objec TId ("5030f7ac721e16c4ab180cdb"), "name": "Chenzhou", "Age": $ >

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 shell code from the Chenzhou age field

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.