What does the $add operator do in MongoDB aggregation?
The $add operator adds numbers or dates together. It sums the values of its arguments and returns the total.
Click to reveal answer
beginner
How do you multiply two fields in a MongoDB aggregation pipeline?
Use the $multiply operator with an array of the fields or values you want to multiply. It returns the product.
Click to reveal answer
intermediate
What happens if you use $divide with a divisor of zero?
MongoDB returns null because division by zero is undefined.
Click to reveal answer
beginner
Write a MongoDB aggregation expression to calculate the total price by multiplying quantity and price fields.
{ $multiply: ["$quantity", "$price"] }
Click to reveal answer
intermediate
Can $add be used to add dates in MongoDB? How?
Yes, $add can add a number (milliseconds) to a date to get a new date. For example, adding 86400000 (milliseconds in a day) to a date adds one day.
Click to reveal answer
Which operator would you use to add two numbers in a MongoDB aggregation?
A$subtract
B$multiply
C$add
D$divide
✗ Incorrect
The $add operator sums numbers or dates.
What does { $multiply: [2, 3, 4] } return?
A9
B24
C6
D14
✗ Incorrect
It multiplies 2 × 3 × 4 = 24.
What is the result of { $divide: [10, 0] } in MongoDB?
Anull
B0
CInfinity
DError
✗ Incorrect
MongoDB returns null for division by zero.
How would you add 5 days to a date field startDate?
A{ $add: ["$startDate", 432000000] }
B{ $multiply: ["$startDate", 5] }
C{ $add: ["$startDate", 5] }
D{ $divide: ["$startDate", 5] }
✗ Incorrect
5 days × 86400000 ms/day = 432000000 ms added to the date.
Which operator would you use to calculate the average price by dividing total price by quantity?
A$subtract
B$multiply
C$add
D$divide
✗ Incorrect
Use $divide to divide total price by quantity.
Explain how to use the MongoDB $add, $multiply, and $divide operators in aggregation pipelines.
Think about simple math operations and how they apply to fields or values.
You got /3 concepts.
Describe a real-life example where you might use $multiply and $divide in a MongoDB aggregation.
Consider shopping or sales data.
You got /3 concepts.
Practice
(1/5)
1. What does the MongoDB expression { $add: [5, 3] } do?
easy
A. Multiplies 5 and 3 to get 15
B. Adds 5 and 3 to get 8
C. Divides 5 by 3 to get 1.67
D. Subtracts 3 from 5 to get 2
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 B
Quick Check:
$add: [5, 3] = 8 [OK]
Hint: Remember $add sums all values inside the array [OK]
Common Mistakes:
Confusing $add with $multiply
Thinking $add subtracts values
Assuming $add divides numbers
2. Which of the following is the correct syntax to multiply 4 by 7 using MongoDB arithmetic expressions?
easy
A. { $multiply: [4, 7] }
B. { $multiply: 4, 7 }
C. { $multiply: (4, 7) }
D. { multiply: [4, 7] }
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 A
Quick Check:
Correct syntax uses $multiply with array [OK]
Hint: Use square brackets [] for values in $multiply [OK]
Common Mistakes:
Omitting $ before multiply
Using parentheses instead of brackets
Passing values without array brackets
3. Given the MongoDB aggregation expression { $divide: [ { $multiply: [10, 5] }, 2 ] }, what is the result?
medium
A. 25
B. 100
C. 2.5
D. 50
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 A
Quick Check:
(10 * 5) / 2 = 25 [OK]
Hint: Calculate inner expressions first, then outer [OK]
Common Mistakes:
Dividing 10 by 5 instead of multiplying
Forgetting to divide after multiplication
Mixing order of operations
4. Identify the error in this MongoDB expression: { $divide: [10, { $add: 2, 3 }] }
medium
A. There is no error; the expression is correct
B. The $divide operator cannot divide numbers
C. The numbers 10, 2, and 3 must be strings
D. The $add operator must have an array of values, not separate arguments
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 D
Quick Check:
$add needs array syntax [OK]
Hint: Always use square brackets [] for $add values [OK]
Common Mistakes:
Using commas instead of array brackets
Assuming $divide cannot divide numbers
Passing numbers as strings
5. You want to calculate the average price of items stored in a MongoDB collection using aggregation. Which expression correctly calculates the average price if you have total price as { $add: ["$price1", "$price2", "$price3"] } and want to divide by 3?