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

CategoryUpdate Field
MongoDB Version2.6+
Example Languagemongosh (Shell)

📝 Syntax

{ $mul: { <field>: <factor> } }

⚙️ Parameters

fieldThe numeric field to multiply.
multiplierThe 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

📚 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏