ANNOUNCEMENT: Voyage AI joins MongoDB to power more accurate and trustworthy AI applications on Atlas.
Learn more
Menu Docs

$mul

$mul

Multiply the value of a field by a number. To specify a $mul expression, use the following prototype:

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

The field to update must contain a numeric value.

Para especificar um <field> em um documento incorporado ou em uma array, use a notação de ponto.

A partir do MongoDB 5.0, mongod não gera mais um erro ao usar um operador de atualização como $mul com uma expressão de operando vazia ( { } ). Uma atualização vazia não resulta em alteração e nenhuma entrada no oplog é criada (o que significa que é sem operação).

A partir do MongoDB 5.0, os operadores de atualização processam campos de documento com nomes baseados em cadeia de caracteres em ordem lexicográfica. Os campos com nomes numéricos são processados em ordem numérica. Consulte Atualizar Comportamento de Operadores para detalhes.

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 é uma operação atômica dentro de um único documento.

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-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

Observação

  • If the product of two 32-bit integers exceeds the maximum value for a 32-bit integer, the result is a 64-bit integer.

  • Integer operations of any type that exceed the maximum value for a 64-bit integer produce an error.

Crie a coleção products:

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

In the following operation, db.collection.updateOne() updates the document. The $mul operator multiplies the price field by 1.25 and the quantity field by 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

  • quantity is the original value, 25, multiplied by 2

{ _id: 1, item: 'Hats', price: Decimal128("13.7375"), quantity: 50 }

Adicione o seguinte documento à coleção 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.products.updateOne(
{ _id: 2 },
{ $mul: { price: Decimal128("100") } }
)

The db.collection.updateOne() operation

  • inserts the price field

  • sets Decimal128("0")

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

The price field has the same type, Decimal128, as the multiplier.

Adicione o seguinte documento à coleção 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.products.updateOne(
{ _id: 3 },
{ $mul: { price: Int32(5) } }
)

A operação resulta no seguinte documento:

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

The value in the price field is of type Decimal128. See Multiplication Type Conversion Rules for details.

Veja também: