0
0
MongoDBquery~5 mins

Arithmetic expressions ($add, $multiply, $divide) in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
What does { $multiply: [2, 3, 4] } return?
A9
B24
C6
D14
What is the result of { $divide: [10, 0] } in MongoDB?
Anull
B0
CInfinity
DError
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] }
Which operator would you use to calculate the average price by dividing total price by quantity?
A$subtract
B$multiply
C$add
D$divide
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.