Navigation
This version of the manual is no longer supported.

$inc

$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".

←   $each $isolated  →