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