On this page本页内容
$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>
,请使用点表示法。
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. 从MongoDB 5.0开始,更新运算符以词典顺序处理具有基于字符串的名称的文档字段。Fields with numeric names are processed in numeric order. 具有数字名称的字段按数字顺序处理。See Update Operators Behavior for details.有关详细信息,请参阅更新运算符行为。
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
将创建该字段,并将该值设置为零,其数值类型与乘数相同。
$mul
is an atomic operation within a single document.是单个文档中的原子操作。
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 |
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
quantity
{ _id: 1, item: 'Hats', price: Decimal128("13.7375"), quantity: 50 }
$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()
操作
price
fieldprice
字段Decimal128("0")
{ "_id" : 2, "item" : "Unknown", "price" : NumberLong(0) }
The price
field has the same type, Decimal128, as the multiplier.price
字段具有与乘数相同的类型,Decimal128
。
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
字段中的值的类型为Decimal128。See Multiplication Type Conversion Rules for details.有关详细信息,请参阅乘法类型转换规则。