$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 ofamount
for the first matching document in the collection wherefield
equalsvalue
:To update all matching documents in the collection, specify
multi:true
in theupdate()
method:The first
update()
operation increments the value of theage
field by1
for all documents in the collection that have anage
field equal to20
. The second operation increments the value of theage
field by2
for all documents in the collection with thename
field equal to"John"
.