发布于 2015-09-14 14:54:17 | 109 次阅读 | 评论: 0 | 来源: 网络整理

$inc

The $inc operator increments a value of a field by a specified amount. If the field does not exist, $inc sets the field to the specified amount. $inc accepts positive and negative incremental amounts.

The following example increments the value of field1 by the value of amount for the first matching document in the collection where field equals value:

db.collection.update( { field: value },
                      { $inc: { field1: amount } } );

To update all matching documents in the collection, specify multi:true in the update() method:

db.collection.update( { age: 20 }, { $inc: { age: 1 } }, { multi: true } );
db.collection.update( { name: "John" }, { $inc: { age: 2 } }, { multi: true } );

The first update() operation increments the value of the age field by 1 for all documents in the collection that have an age field equal to 20. The second operation increments the value of the age field by 2 for all documents in the collection with the name field equal to "John".

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务