Definition定义
$mulMultiply the value of a field by a number. To specify a将字段的值乘以一个数字。要指定$mulexpression, 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, 从MongoDB 5.0开始,当您使用带有空操作数表达式(mongod no longer raises an error when you use an update operator like $mul with an empty operand expression ( { } ). { })的$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. Fields with numeric names are processed in numeric order. See Update Operators Behavior for details.从MongoDB 5.0开始,更新运算符按字典顺序处理具有基于字符串的名称的文档字段。具有数字名称的字段按数字顺序处理。有关详细信息,请参阅更新运算符行为。
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, Double, Decimal128) may result in conversion of numeric type. For multiplication with values of mixed numeric types, the following type conversion rules apply:与混合数字类型(32位整数、64位整数、Double、Decimal128)的值相乘可能会导致数字类型的转换。对于混合数字类型的值的乘法,适用以下类型转换规则:
| Data Type | 32-bit Integer | 64-bit Integer | Double | Decimal128 |
|---|---|---|---|---|
| 32-bit Integer | 32-bit or 64-bit Integer | 64-bit Integer | Decimal128 | |
| 64-bit Integer | 64-bit Integer | 64-bit Integer | Decimal128 | |
| Decimal128 | ||||
| Decimal128 | Decimal128 | Decimal128 | Decimal128 | Decimal128 |
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 quantity field by 2:$mul运算符将price字段乘以1.25,将quantity字段乘以2:
db.products.updateOne(
{ _id: 1 },
{ $mul:
{
price: Decimal128( "1.25" ),
quantity: 2
}
}
)
In the updated document:在更新的文件中:
priceis the original value, 10.99, multiplied by 1.25是原始值10.99乘以1.25quantityis 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运算符应用于不存在的字段
$mul Operator to a Non-existing FieldAdd 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插入pricefieldprice字段sets Decimal128("0")设置Decimal128("0")
{ "_id" : 2, "item" : "Unknown", "price" : Long(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. See Multiplication Type Conversion Rules for details.price字段中的值的类型为Decimal128。详见乘法类型转换规则。