$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, 从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.有关详细信息,请参阅更新运算符行为。
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. For multiplication with values of mixed numeric types, the following type conversion rules apply:与混合数字类型(32位整数、64位整数、浮点)的值相乘可能会导致数字类型的转换。对于与混合数字类型的值相乘,适用以下类型转换规则:
32-bit Integer | 64-bit Integer | Float | |
---|---|---|---|
Float | |||
64-bit Integer | 64-bit Integer | 64-bit Integer | Float |
Float | Float | Float | Float |
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:在更新后的文档中:
price
is the original value, 10.99, multiplied by 1.25是原始值10.99乘以1.25quantity
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
运算符应用于不存在的字段
$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插入price
fieldprice
字段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
字段中的值的类型为Decimal128
。See Multiplication Type Conversion Rules for details.有关详细信息,请参阅乘法类型转换规则。