On this page本页内容
$mul
¶Multiply the value of a field by a number. 将一个字段的值乘以一个数字。To specify a 要指定$mul
expression, use the following prototype:$mul
表达式,请使用以下原型:
The field to update must contain a numeric value.要更新的字段必须包含一个数值。
To specify a 若要在嵌入式文档或数组中指定一个<field>
in an embedded document or in an array, use dot notation.<field>
,请使用点表示法。
If the field does not exist in a document, 如果文档中不存在该字段,$mul
creates the field and sets the value to zero of the same numeric type as the multiplier.$mul
创建该字段,并将与乘数相同的数值类型的值设置为零。
Multiplication with values of mixed numeric types (32-bit integer, 64-bit integer, float) may result in conversion of numeric type. 与混合数字类型(32位整数、64位整数、浮点)的值相乘可能会导致数字类型的转换。For multiplication with values of mixed numeric types, the following type conversion rules apply:对于与混合数字类型的值相乘,以下类型转换规则适用:
32-bit Integer | 64-bit Integer | Float | |
---|---|---|---|
32-bit Integer | 32-bit or 64-bit Integer | 64-bit Integer | Float |
64-bit Integer | 64-bit Integer | 64-bit Integer | Float |
Float | Float | Float | Float |
Note
Consider a collection 考虑一个集合products
with the following document:products
带有以下文档:
The following 下面是更新文档的db.collection.update()
operation updates the document, using the $mul
operator to multiply the price
by 1.25
and the qty
field by 2
:db.collection.update()
操作,使用$mul
运算符将price
乘以1.25
,将qty
字段乘以2
:
The operation results in the following document, where the new value of 该操作产生以下文档,其中新的price
reflects the original value 10.99
multiplied by 1.25
and the new value of qty
reflects the original value of 25
multipled by 2
:price
值反映原始值10.99
乘以1.25
,新的数量值反映原始值25
乘以2
:
$mul
Operator to a Non-existing Field$mul
运算符应用于不存在的字段¶Consider a collection 考虑一个集合products
with the following document:products
带有以下文档:
The following 下面是更新文档的db.collection.update()
operation updates the document, applying the $mul
operator to the field price
that does not exist in the document:db.collection.update()
操作,将$mul
运算符应用于文档中不存在的字段price
:
The operation results in the following document with a 该操作将导致以下文档中的price
field set to value 0 of numeric type NumberLong, the same type as the multiplier:price
字段设置为数值类型NumberLong的值0,与乘数的类型相同:
Consider a collection 考虑一个集合products
with the following document:products
带有以下文档:
The following 下面的db.collection.update()
operation uses the $mul
operator to multiply the value in the price
field NumberLong(10) by NumberInt(5):db.collection.update()
操作使用$mul
运算符将price
字段NumberLong(10)中的值乘以numberrint(5):
The operation results in the following document:操作结果如下所示:
The value in the price
field is of type NumberLong. price
字段中的值为NumberLong类型。See Multiplication Type Conversion Rules for details.有关详细信息,请参阅乘法类型转换规则。
See also参阅