Skip to content

DOCS-1077 add missing multi parameter to update examples #646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions source/reference/operator/inc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"``.