On this page本页内容
$inc
¶The $inc
operator increments a field by a specified value and has the following form:$inc
运算符按指定值递增字段,其形式如下:
To specify a 若要在嵌入式文档或数组中指定一个<field>
in an embedded document or in an array, use dot notation.<field>
,请使用点表示法。
The $inc
operator accepts positive and negative values.$inc
运算符接受正值和负值。
If the field does not exist, 如果该字段不存在,$inc
creates the field and sets the field to the specified value.$inc
将创建该字段并将该字段设置为指定值。
Use of the 在具有$inc
operator on a field with a null value will generate an error.null
值的字段上使用$inc
运算符将生成错误。
$inc
is an atomic operation within a single document.$inc
是单个文档中的原子操作。
Consider a collection 考虑一个集合products
with the following document:products
带有以下文档:
The following 下面的update()
operation uses the $inc
operator to decrease the quantity
field by 2
(i.e. increase by -2
) and increase the "metrics.orders"
field by 1
:update()
操作使用$inc
运算符将quantity
字段减少2(即增加-2
),并将"metrics.orders"
字段增加1
:
The updated document would resemble:更新后的文件类似于:
See also参阅