Concept Flow - $mul operator for multiplication
Start Update
Find Document
Apply $mul
Multiply Field by Value
Save Updated Document
End Update
The $mul operator multiplies a field's value by a specified number during an update operation.
db.products.updateOne(
{ _id: 1 },
{ $mul: { price: 1.1 } }
)| Step | Action | Field 'price' Value Before | Multiplier | Field 'price' Value After | Notes |
|---|---|---|---|---|---|
| 1 | Find document with _id:1 | 100 | N/A | 100 | Initial price is 100 |
| 2 | Apply $mul operator | 100 | 1.1 | 110 | 100 * 1.1 = 110 |
| 3 | Save updated document | N/A | N/A | 110 | Document updated with new price |
| 4 | End update | 110 | N/A | 110 | Update operation complete |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| price | 100 | 100 | 110 | 110 | 110 |
$mul operator syntax:
{ $mul: { <field>: <number> } }
Multiplies the field's current value by the number.
If field missing, treated as 0.
Used in update operations to scale numeric fields.