$mul (Multiply)
Last updated: 2026-10-09
$mul (Multiply)
$mul multiplies a field value by the given factor. `{ $mul: { price: 0.9 } }` applies a 10% discount; `{ $mul: { quantity: 2 } }` doubles. For a missing field the result is treated as multiplied-by-zero, so the field is created as 0. Non-numeric fields throw. Atomic; handy for price or weight scaling.
| Category | Update Field |
|---|---|
| MongoDB Version | 2.6+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $mul: { <field>: <factor> } }
⚙️ Parameters
| field | The numeric field to multiply. |
|---|---|
| multiplier | The multiplication factor, decimal or negative allowed. |
Returns:The update result
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $mul: 10% off the price
// 100 becomes 90
db.products.updateOne(
{ _id: 1 },
{ $mul: { price: 0.9 } }
)
// Double the quantity
db.products.updateOne(
{ _id: 2 },
{ $mul: { qty: 2 } }
)
Output:
// price is multiplied by 0.9 and qty by 2; missing fields are created as 0
📌 Notes
- A missing field is created as 0 (multiply-by-zero semantics)
- Non-numeric fields throw an error
- Like $inc, it is an atomic operation
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QWhat happens if the $mul field is missing?
AIt is treated as multiplied by zero, so the field is created as 0.
QDoes $mul handle floats?
AYes; precision depends on the BSON type.
QWhat is the difference between $mul and $inc?
A$inc adds or subtracts; $mul multiplies or divides.