$mul

On this page本页内容

Definition定义

$mul

Multiply the value of a field by a number. 将一个字段的值乘以一个数字。To specify a $mul expression, use the following prototype:要指定$mul表达式,请使用以下原型:

{ $mul: { <field1>: <number1>, ... } }

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>,请使用点表示法

Behavior行为

Missing 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创建该字段,并将与乘数相同的数值类型的值设置为零。

Atomic原子的

$mul is an atomic operation within a single document.$mul是单个文档中的一个原子操作。

Mixed Type混合类型

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 Integer64-bit IntegerFloat
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

  • If the product of two 32-bit integers exceeds the maximum value for a 32-bit integer, the result is a 64-bit integer.如果两个32位整数的乘积超过32位整数的最大值,则结果为64位整数。
  • Integer operations of any type that exceed the maximum value for a 64-bit integer produce an error.超过64位整数最大值的任何类型的整数操作都会产生错误。

Examples示例

Multiply the Value of a Field将字段的值相乘

Consider a collection products with the following document:考虑一个集合products带有以下文档:

{ "_id" : 1, "item" : "ABC", "price" : NumberDecimal("10.99"), "qty" : 25 }

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

db.products.update(
   { _id: 1 },
   { $mul: { price: NumberDecimal("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

{ "_id" : 1, "item" : "ABC", "price" : NumberDecimal("13.7375"), "qty" : 50 }

Apply $mul Operator to a Non-existing Field$mul运算符应用于不存在的字段

Consider a collection products with the following document:考虑一个集合products带有以下文档:

{ _id: 2,  item: "Unknown" }

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

db.products.update(
   { _id: 2 },
   { $mul: { price: NumberLong(100) } }
)

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,与乘数的类型相同:

{ "_id" : 2, "item" : "Unknown", "price" : NumberLong(0) }

Multiply Mixed Numeric Types乘法混合数字类型

Consider a collection products with the following document:考虑一个集合products带有以下文档:

{ _id: 3,  item: "XYZ", price: NumberLong(10) }

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)

db.products.update(
   { _id: 3 },
   { $mul: { price: NumberInt(5) } }
)

The operation results in the following document:操作结果如下所示:

{ "_id" : 3, "item" : "XYZ", "price" : NumberLong(50) }

The value in the price field is of type NumberLong. price字段中的值为NumberLong类型。See Multiplication Type Conversion Rules for details.有关详细信息,请参阅乘法类型转换规则