0
0
MongoDBquery~3 mins

Why $mul operator for multiplication in MongoDB? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could multiply thousands of prices with one simple command instead of hours of manual work?

The Scenario

Imagine you have a list of products with prices stored in a spreadsheet. You want to increase all prices by 10%. Doing this manually means opening each row and multiplying the price by 1.1 one by one.

The Problem

This manual method is slow and boring. It's easy to make mistakes, like skipping a row or typing the wrong number. If you have thousands of products, it becomes a huge headache.

The Solution

The $mul operator in MongoDB lets you multiply a field's value directly in the database. You can update many records at once with a simple command, saving time and avoiding errors.

Before vs After
Before
for each product:
  product.price = product.price * 1.1
  save product
After
db.products.updateMany({}, { $mul: { price: 1.1 } })
What It Enables

With $mul, you can quickly and safely adjust numeric values across many records in one step.

Real Life Example

A store owner wants to apply a 10% price increase to all items after supplier costs rise. Using $mul, they update all prices instantly without opening each product.

Key Takeaways

Manual updates are slow and error-prone.

$mul multiplies field values directly in the database.

This saves time and reduces mistakes when updating many records.