0
0
MongoDBquery~10 mins

$mul operator for multiplication in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
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.
Execution Sample
MongoDB
db.products.updateOne(
  { _id: 1 },
  { $mul: { price: 1.1 } }
)
This multiplies the 'price' field of the product with _id 1 by 1.1 (increasing price by 10%).
Execution Table
StepActionField 'price' Value BeforeMultiplierField 'price' Value AfterNotes
1Find document with _id:1100N/A100Initial price is 100
2Apply $mul operator1001.1110100 * 1.1 = 110
3Save updated documentN/AN/A110Document updated with new price
4End update110N/A110Update operation complete
💡 Update ends after multiplying 'price' by 1.1 and saving the document.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
price100100110110110
Key Moments - 2 Insights
Why does the 'price' field change from 100 to 110?
Because the $mul operator multiplies the current 'price' value (100) by 1.1, resulting in 110 as shown in execution_table step 2.
What happens if the field does not exist in the document?
The $mul operator will create the field and set it to 0 multiplied by the value, which results in 0. This is not shown in the current example but is important to know.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'price' after step 2?
A110
B100
C1.1
D0
💡 Hint
Check the 'Field price Value After' column in step 2 of the execution_table.
At which step is the document saved with the updated price?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the action 'Save updated document' in the execution_table.
If the multiplier was 2 instead of 1.1, what would be the 'price' after step 2?
A110
B200
C100
D50
💡 Hint
Multiply the initial price 100 by 2 as per the $mul operator logic.
Concept Snapshot
$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.
Full Transcript
The $mul operator in MongoDB updates multiplies a numeric field by a specified value. The process starts by finding the document to update. Then, $mul multiplies the current field value by the given multiplier. The updated value replaces the old one, and the document is saved. For example, multiplying a price of 100 by 1.1 results in 110. If the field does not exist, it is treated as zero. This operator is useful for scaling prices, quantities, or other numeric data in documents.