$mul

Definition

  • $mul

New in version 2.6.

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

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

The field to update must contain a numeric value.

To specify a <field> in an embedded document or in an array, usedot notation.

Behavior

Missing Field

If the field does not exist in a document, $mul creates thefield and sets the value to zero of the same numeric type as themultiplier.

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. Formultiplication with values of mixed numeric types, the following typeconversion 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 valuefor a 32-bit integer, the result is a 64-bit integer.
  • Integer operations of any type that exceed the maximum value for a64-bit integer produce an error.

Examples

Multiply the Value of a Field

Consider a collection products with the following document:

  1. { "_id" : 1, "item" : "ABC", "price" : NumberDecimal("10.99"), "qty" : 25 }

The following db.collection.update() operation updates thedocument, using the $mul operator to multiply the priceby 1.25 and the qty field by 2:

  1. db.products.update(
  2. { _id: 1 },
  3. { $mul: { price: NumberDecimal("1.25"), qty: 2 } }
  4. )

The operation results in the following document, where the new value ofprice reflects the original value 10.99 multiplied by 1.25and the new value of qty reflects the original value of 25multipled by 2:

  1. { "_id" : 1, "item" : "ABC", "price" : NumberDecimal("13.7375"), "qty" : 50 }

Apply $mul Operator to a Non-existing Field

Consider a collection products with the following document:

  1. { _id: 2, item: "Unknown" }

The following db.collection.update() operation updates thedocument, applying the $mul operator to the field pricethat does not exist in the document:

  1. db.products.update(
  2. { _id: 2 },
  3. { $mul: { price: NumberLong(100) } }
  4. )

The operation results in the following document with a price fieldset to value 0 of numeric type NumberLong, the same type asthe multiplier:

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

Multiply Mixed Numeric Types

Consider a collection products with the following document:

  1. { _id: 3, item: "XYZ", price: NumberLong(10) }

The following db.collection.update() operation uses the$mul operator to multiply the value in the price fieldNumberLong(10) by NumberInt(5):

  1. db.products.update(
  2. { _id: 3 },
  3. { $mul: { price: NumberInt(5) } }
  4. )

The operation results in the following document:

  1. { "_id" : 3, "item" : "XYZ", "price" : NumberLong(50) }

The value in the price field is of type NumberLong. SeeMultiplication Type Conversion Rules for details.

See also

db.collection.update(),db.collection.findAndModify()