0
0
MongoDBquery~10 mins

Arithmetic expressions ($add, $multiply, $divide) in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Arithmetic expressions ($add, $multiply, $divide)
Start with input documents
Apply $add, $multiply, $divide
Calculate values step-by-step
Create new fields with results
Output transformed documents
The flow starts with input documents, applies arithmetic expressions like $add, $multiply, and $divide to compute new values, then outputs documents with these calculated fields.
Execution Sample
MongoDB
db.sales.aggregate([
  { $project: {
      total: { $add: ["$price", "$tax"] },
      doublePrice: { $multiply: ["$price", 2] },
      pricePerItem: { $divide: ["$price", "$quantity"] }
    }
  }
])
This aggregation calculates total price with tax, doubles the price, and finds price per item for each sales document.
Execution Table
StepDocument$add Calculation$multiply Calculation$divide CalculationOutput Document
1{"price": 100, "tax": 10, "quantity": 5}100 + 10 = 110100 * 2 = 200100 / 5 = 20{"total": 110, "doublePrice": 200, "pricePerItem": 20}
2{"price": 50, "tax": 5, "quantity": 2}50 + 5 = 5550 * 2 = 10050 / 2 = 25{"total": 55, "doublePrice": 100, "pricePerItem": 25}
3{"price": 80, "tax": 8, "quantity": 4}80 + 8 = 8880 * 2 = 16080 / 4 = 20{"total": 88, "doublePrice": 160, "pricePerItem": 20}
ExitNo more documents---Aggregation ends
💡 All documents processed, aggregation pipeline completes.
Variable Tracker
VariableStartAfter Doc 1After Doc 2After Doc 3Final
$add result (total)-110558888
$multiply result (doublePrice)-200100160160
$divide result (pricePerItem)-20252020
Key Moments - 3 Insights
Why does $divide return a decimal if inputs are integers?
In the execution_table, $divide calculates price divided by quantity. If the division is exact, it returns an integer; otherwise, it returns a decimal. Here, all divisions are exact, so results are integers.
Can $add combine more than two values?
Yes, $add can sum multiple values. In the example, it adds price and tax, but you could add more fields or numbers by listing them inside the array.
What happens if quantity is zero in $divide?
Dividing by zero causes an error. The aggregation would fail or skip that document depending on error handling. This example assumes quantity is never zero.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the $multiply result for the second document?
A55
B100
C25
D200
💡 Hint
Check the $multiply Calculation column for Step 2 in the execution_table.
At which step does the $add calculation equal 110?
AStep 1
BStep 2
CStep 3
DExit
💡 Hint
Look at the $add Calculation column in the execution_table to find 110.
If quantity was zero in the first document, what would happen to $divide?
AIt would return null
BIt would return zero
CIt would cause an error
DIt would skip division
💡 Hint
Refer to key_moments about division by zero behavior.
Concept Snapshot
MongoDB arithmetic expressions:
- $add sums values: {$add: [val1, val2, ...]}
- $multiply multiplies: {$multiply: [val1, val2, ...]}
- $divide divides: {$divide: [val1, val2]}
Use in aggregation to compute new fields.
Division by zero causes errors.
Supports numbers and field references.
Full Transcript
This visual execution shows how MongoDB arithmetic expressions $add, $multiply, and $divide work inside an aggregation pipeline. Each input document has fields price, tax, and quantity. The pipeline calculates total as price plus tax, doublePrice as price times two, and pricePerItem as price divided by quantity. Step by step, the calculations are done for each document, and the results are added as new fields. The variable tracker shows how these calculated values change after each document. Key moments clarify common confusions like division by zero and multiple additions. The quiz tests understanding of specific calculation results and error cases. This helps beginners see exactly how MongoDB computes arithmetic expressions in aggregation.