0
0
MongoDBquery~20 mins

Arithmetic expressions ($add, $multiply, $divide) in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Arithmetic Expressions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this MongoDB aggregation?
Given a collection with documents like {"value1": 10, "value2": 5}, what will be the result of this aggregation pipeline stage?
{ $project: { result: { $add: ["$value1", "$value2"] } } }
MongoDB
{ $project: { result: { $add: ["$value1", "$value2"] } } }
A[{ "result": 5 }]
B[{ "result": 50 }]
C[{ "result": 15 }]
D[{ "result": 2 }]
Attempts:
2 left
💡 Hint
Think about what $add does with two numbers.
query_result
intermediate
2:00remaining
What does this $multiply expression return?
For documents with fields {"a": 4, "b": 3}, what is the output of this aggregation stage?
{ $project: { product: { $multiply: ["$a", "$b"] } } }
MongoDB
{ $project: { product: { $multiply: ["$a", "$b"] } } }
A[{ "product": 0 }]
B[{ "product": 7 }]
C[{ "product": 1.33 }]
D[{ "product": 12 }]
Attempts:
2 left
💡 Hint
Multiply the two numbers.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this $divide expression?
Identify which aggregation stage will cause a syntax error when trying to divide two fields:
{ $project: { ratio: { $divide: ["$num", "$den"] } } }
MongoDB
{ $project: { ratio: { $divide: ["$num", "$den"] } } }
A{ $project: { ratio: { $divide: "$num" } } }
B{ $project: { ratio: { $divide: ["$num", "$den"] } } }
C{ $project: { ratio: { $divide: ["$num"] } } }
D{ $project: { ratio: { $divide: ["$num", "$den", "$extra"] } } }
Attempts:
2 left
💡 Hint
Check the number of arguments $divide expects.
query_result
advanced
2:00remaining
What is the output of this nested arithmetic expression?
Given documents with {"x": 8, "y": 2, "z": 4}, what is the result of this aggregation stage?
{ $project: { result: { $add: [ { $multiply: ["$x", "$y"] }, { $divide: ["$z", 2] } ] } } }
MongoDB
{ $project: { result: { $add: [ { $multiply: ["$x", "$y"] }, { $divide: ["$z", 2] } ] } } }
A[{ "result": 18 }]
B[{ "result": 20 }]
C[{ "result": 16 }]
D[{ "result": 12 }]
Attempts:
2 left
💡 Hint
Calculate multiplication and division first, then add.
🧠 Conceptual
expert
2:00remaining
Why does this $divide operation return null?
Consider documents with {"a": 10, "b": 0}. What is the output of this aggregation stage?
{ $project: { result: { $divide: ["$a", "$b"] } } }

Why does it return null instead of an error?
MongoDB
{ $project: { result: { $divide: ["$a", "$b"] } } }
AMongoDB throws a syntax error for division by zero.
BMongoDB returns null to avoid runtime errors when dividing by zero.
CMongoDB returns Infinity for division by zero.
DMongoDB returns zero when dividing by zero.
Attempts:
2 left
💡 Hint
Think about how MongoDB handles invalid math operations.