diff --git a/source/reference/operator/inc.txt b/source/reference/operator/inc.txt index c6a44cf8b2b..88cdf88eaa4 100644 --- a/source/reference/operator/inc.txt +++ b/source/reference/operator/inc.txt @@ -6,30 +6,31 @@ $inc .. operator:: $inc - The :operator:`$inc` operator increments a value by a specified - amount if field is present in the document. If the field does not - exist, :operator:`$inc` sets field to the number value. For - example: + The :operator:`$inc` operator increments a value of a field by a + specified amount. If the field does not exist, :operator:`$inc` sets + the field to the specified amount. :operator:`$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``: .. code-block:: javascript - db.collection.update( { field: value }, { $inc: { field1: amount } } ); + db.collection.update( { field: value }, + { $inc: { field1: amount } } ); - In this example, for documents in ``collection`` where - ``field`` has the value ``value``, the value of ``field1`` - increments by the value of ``amount``. The above operation only - increments the *first* matching document *unless* you specify - multi-update: + To update all matching documents in the collection, specify + ``multi:true`` in the :method:`~db.collection.update()` method: .. code-block:: javascript - db.collection.update( { age: 20 }, { $inc: { age: 1 } } ); - db.collection.update( { name: "John" }, { $inc: { age: 1 } } ); - - In the first example all documents that have an ``age`` field with - the value of ``20``, the operation increases ``age`` field by - one. In the second example, in all documents where the ``name`` - field has a value of ``John`` the operation increases the value - of the ``age`` field by one. + db.collection.update( { age: 20 }, { $inc: { age: 1 } }, { multi: true } ); + db.collection.update( { name: "John" }, { $inc: { age: 2 } }, { multi: true } ); - :operator:`$inc` accepts positive and negative incremental amounts. + The first :method:`~db.collection.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"``.