MongoDB Learning Note 9 Deep MongoDB Update action: modifier $inc

Source: Internet
Author: User
Tags findone modifier mongodb update

Ongodb Chinese file in the database after the Update method updated the document, the Update method has two parameters, such as

Update (ARGS1,ARGS2)

ARGS1 refers to the condition of querying a document;

ARGS2 refers to what changes are made to the document being queried;

$inc used to increase the value of the key used, if the key is not present to create;

$INC the key values that can only be modified must be integer, Long Integer, and double-precision floating-point types.


Use $inc to increase the key value:

> db.post.findone ({"id": 0})    {              "_id"  : objectid ("54a530c3ff0df3732bac167f"),              "id"  : 0,              "name"  :  "Joe",              "Age"  : 65,             "comments"  : {                      "1"  : 4,                      "2"  : 2,                      "3"  : 3             }    }    >  Db.post.update ({"id": 0}, {$inc: {"Age": 6}})     writeresult ({  "nmatched"  :  1,  "nupserted"  : 0,  "nmodified"  : 1 })

Results after modification:  

> db.post.findone ({"id": 0})     {              "_id"  : objectid ("54a530c3ff0df3732bac167f"),              "id"  : 0,              "name"  :  "Joe",              "Age"  : 71,             " Comments " : {                      "1"  : 4,                      "2"  : 2,                      "3"  :  3             }    } 

Use $inc to reduce key values:

> db.post.update ({"id": 0}, {$inc: {"Age": -16}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

The modified result:

> db.post.findone ({"id": 0})     {              "_id"  : objectid ("54a530c3ff0df3732bac167f"),              "id"  : 0,              "name"  :  "Joe",              "Age"  : 55,             " Comments " : {                      "1"  : 4,                      "2"  : 2,                      "3"  :  3            }    }    > 



This article is from the "Margin with Wish" blog, please be sure to keep this source http://281816327.blog.51cto.com/907015/1598401

MongoDB Learning Note 9 Deep MongoDB Update action: modifier $inc

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.