$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行为

Starting in MongoDB 5.0, mongod no longer raises an error when you use an update operator like $mul with an empty operand expression ( { } ). 从MongoDB 5.0开始,当使用带有空操作数表达式({ })的更新运算符(如$mul)时,mongod不再引发错误。An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).空更新不会导致任何更改,也不会创建oplog条目(这意味着该操作是无操作)。

Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. 从MongoDB 5.0开始,更新运算符以词典顺序处理具有基于字符串的名称的文档字段。Fields with numeric names are processed in numeric order. 具有数字名称的字段按数字顺序处理。See Update Operators Behavior for details.有关详细信息,请参阅更新运算符行为

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.是单个文档中的原子操作。

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 Integer32-bit or 64-bit Integer64-bit IntegerFloat
64-bit Integer64-bit Integer64-bit IntegerFloat
FloatFloatFloatFloat
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乘以字段的值

Create the products collection:创建products集合:

db.products.insertOne(
   { "_id" : 1, "item" : "Hats", "price" : Decimal128("10.99"), "quantity" : 25 }
)

In the following operation, db.collection.updateOne() updates the document. 在以下操作中,db.collection.updateOne()更新文档。The $mul operator multiplies the price field by 1.25 and the price field by 2:$mul运算符将price字段乘以1.25,将price字段乘以2

db.products.updateOne(
   { _id: 1 },
   { $mul:
      {
         price: Decimal128( "1.25" ),
         quantity: 2
       }
   }
)

In the updated document:在更新文件中:

  • price is the original value, 10.99, multiplied by 1.25是原始值10.99乘以1.25
  • quantity is the original value, 25, multiplied by 2是原始值25乘以2
{ _id: 1, item: 'Hats', price: Decimal128("13.7375"), quantity: 50 }

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

Add the following document to the products collection:将以下文档添加到products集合:

db.products.insertOne( { _id: 2,  item: "Unknown" } )

In the following operation, db.collection.updateOne() attempts to apply the $mul operator to a field that is not in the document:在以下操作中,db.collection.updateOne()尝试将$mul运算符应用于文档中不存在的字段:

db.products.updateOne(
   { _id: 2 },
   { $mul: { price: Decimal128("100") } }
)

The db.collection.updateOne() operationdb.collection.updateOne()操作

  • inserts the price field插入price字段
  • sets 设置为Decimal128("0")
{ "_id" : 2, "item" : "Unknown", "price" : NumberLong(0) }

The price field has the same type, Decimal128, as the multiplier.price字段具有与乘数相同的类型,Decimal128

Multiply Mixed Numeric Types多重混合数字类型

Add the following document to the products collection:将以下文档添加到products集合:

db.products.insertOne( { _id: 3,  item: "Scarf", price: Decimal128("10") } )

In the following operation, db.collection.updateOne() uses the $mul operator to multiply the value in the price field Decimal128(10) by Int32(5):在以下操作中,db.collection.updateOne()使用$mul运算符将price字段Decimal128(10)中的值乘以Int32(5)

db.products.updateOne(
   { _id: 3 },
   { $mul: { price: Int32(5) } }
)

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

{ _id: 3, item: 'Scarf', price: Decimal128("50") }

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

←  $max$rename →