Arithmetic expressions ($add, $multiply, $divide) in MongoDB - Time & Space Complexity
When using arithmetic expressions like $add, $multiply, and $divide in MongoDB, it's important to know how the time to compute results grows as the data size grows.
We want to understand how the number of calculations changes when we process more documents.
Analyze the time complexity of the following MongoDB aggregation snippet.
db.sales.aggregate([
{
$set: {
totalPrice: { $multiply: ["$price", "$quantity"] }
}
},
{
$set: {
discountPrice: { $divide: ["$totalPrice", 2] }
}
},
{
$set: {
finalPrice: { $add: ["$discountPrice", 10] }
}
}
])
This code calculates total, discounted, and final prices for each sale document.
Look for repeated calculations or loops.
- Primary operation: Arithmetic calculations ($multiply, $divide, $add) done once per document.
- How many times: Once for each document in the collection.
Each document requires a fixed number of arithmetic steps.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 30 (3 operations x 10 documents) |
| 100 | 300 (3 operations x 100 documents) |
| 1000 | 3000 (3 operations x 1000 documents) |
Pattern observation: The total calculations grow directly with the number of documents.
Time Complexity: O(n)
This means the time to compute grows in a straight line as you add more documents.
[X] Wrong: "Arithmetic expressions run instantly no matter how many documents there are."
[OK] Correct: Each document needs its own calculations, so more documents mean more work.
Understanding how simple arithmetic operations scale helps you explain performance in real database queries clearly and confidently.
"What if we added a nested $map to perform arithmetic on an array inside each document? How would the time complexity change?"
Practice
{ $add: [5, 3] } do?Solution
Step 1: Understand the $add operator
The $add operator sums the numbers inside the array.Step 2: Calculate the sum of 5 and 3
5 + 3 equals 8.Final Answer:
Adds 5 and 3 to get 8 -> Option BQuick Check:
$add: [5, 3] = 8 [OK]
- Confusing $add with $multiply
- Thinking $add subtracts values
- Assuming $add divides numbers
Solution
Step 1: Recall correct operator syntax
MongoDB arithmetic operators use $ followed by the operator name and an array of values.Step 2: Check each option
{ $multiply: [4, 7] } uses $multiply with an array [4, 7], which is correct syntax.Final Answer:
{ $multiply: [4, 7] } -> Option AQuick Check:
Correct syntax uses $multiply with array [OK]
- Omitting $ before multiply
- Using parentheses instead of brackets
- Passing values without array brackets
{ $divide: [ { $multiply: [10, 5] }, 2 ] }, what is the result?Solution
Step 1: Calculate the inner $multiply expression
$multiply: [10, 5] equals 50.Step 2: Divide the result by 2
$divide: [50, 2] equals 25.Final Answer:
25 -> Option AQuick Check:
(10 * 5) / 2 = 25 [OK]
- Dividing 10 by 5 instead of multiplying
- Forgetting to divide after multiplication
- Mixing order of operations
{ $divide: [10, { $add: 2, 3 }] }Solution
Step 1: Check $add operator usage
$add requires an array of values, but { $add: 2, 3 } uses separate arguments incorrectly.Step 2: Confirm correct syntax
The correct syntax is { $add: [2, 3] } with square brackets.Final Answer:
The $add operator must have an array of values, not separate arguments -> Option DQuick Check:
$add needs array syntax [OK]
- Using commas instead of array brackets
- Assuming $divide cannot divide numbers
- Passing numbers as strings
{ $add: ["$price1", "$price2", "$price3"] } and want to divide by 3?Solution
Step 1: Understand the average formula
Average = (sum of values) / number of values.Step 2: Check each option
{ $divide: [ { $add: ["$price1", "$price2", "$price3"] }, 3 ] } sums prices then divides by 3, matching the average formula.Final Answer:
{ $divide: [ { $add: ["$price1", "$price2", "$price3"] }, 3 ] } -> Option CQuick Check:
Sum prices then divide by count = average [OK]
- Dividing each price separately then adding
- Multiplying sum by count instead of dividing
- Dividing count by sum instead of sum by count
Ready to prove it?
Pass the test and this pattern moves unknown
