0
0
MongoDBquery~20 mins

$addFields for computed fields in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $addFields 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 $addFields stage?
Given a collection with documents like {"name": "Alice", "scores": [10, 20, 30]}, what will be the result of this aggregation pipeline stage?
{ "$addFields": { "totalScore": { "$sum": "$scores" } }}
MongoDB
db.collection.aggregate([{ "$addFields": { "totalScore": { "$sum": "$scores" } }}])
A[{"name": "Alice", "scores": [10, 20, 30], "totalScore": 60}]
B[{"name": "Alice", "scores": [10, 20, 30], "totalScore": [10, 20, 30]}]
C[{"name": "Alice", "scores": [10, 20, 30], "totalScore": 3}]
D[{"name": "Alice", "scores": [10, 20, 30], "totalScore": null}]
Attempts:
2 left
💡 Hint
Think about what $sum does when applied to an array field.
query_result
intermediate
2:00remaining
What does this $addFields expression compute?
Consider documents with fields {"price": 100, "tax": 0.15}. What will this $addFields stage add?
{ "$addFields": { "finalPrice": { "$multiply": ["$price", { "$add": [1, "$tax"] }] } }}
MongoDB
db.collection.aggregate([{ "$addFields": { "finalPrice": { "$multiply": ["$price", { "$add": [1, "$tax"] }] } }}])
A[{"price": 100, "tax": 0.15, "finalPrice": 100.15}]
B[{"price": 100, "tax": 0.15, "finalPrice": 15}]
C[{"price": 100, "tax": 0.15, "finalPrice": 1.15}]
D[{"price": 100, "tax": 0.15, "finalPrice": 115}]
Attempts:
2 left
💡 Hint
Calculate 100 * (1 + 0.15).
📝 Syntax
advanced
2:00remaining
Which $addFields syntax is valid to add a field 'discounted' as price minus discount?
Choose the correct $addFields stage to compute 'discounted' = price - discount.
A{ "$addFields": { "discounted": { "$subtract": ["$price", "$discount"] } } }
B{ "$addFields": { "discounted": "$price" - "$discount" } }
C{ "$addFields": { "discounted": { "$minus": ["$price", "$discount"] } } }
D{ "$addFields": { "discounted": { "$subtract": "$price" - "$discount" } } }
Attempts:
2 left
💡 Hint
Use the correct MongoDB operator for subtraction with array syntax.
optimization
advanced
2:00remaining
How to optimize adding multiple computed fields in one $addFields stage?
You want to add 'total' as sum of 'a' and 'b', and 'average' as their average. Which $addFields stage is best?
A{ "$addFields": { "total": { "$add": ["$a", "$b"] } } }, { "$addFields": { "average": { "$divide": [{ "$add": ["$a", "$b"] }, 2] } } }
B[{ "$addFields": { "total": { "$add": ["$a", "$b"] } } }, { "$addFields": { "average": { "$divide": [{ "$add": ["$a", "$b"] }, 2] } } }]
C{ "$addFields": { "total": { "$add": ["$a", "$b"] }, "average": { "$divide": [{ "$add": ["$a", "$b"] }, 2] } } }
D{ "$addFields": { "average": { "$divide": [{ "$add": ["$a", "$b"] }, 2] } }, { "$addFields": { "total": { "$add": ["$a", "$b"] } } } }
Attempts:
2 left
💡 Hint
Combine all computed fields in a single $addFields stage for efficiency.
🧠 Conceptual
expert
2:00remaining
What happens if $addFields uses a field name that already exists?
If a document has a field 'status' and you use $addFields to add 'status' with a new value, what is the result?
AMongoDB throws an error because field names must be unique.
BThe existing 'status' field is replaced with the new value from $addFields.
CThe new 'status' field is added alongside the existing one as an array.
DThe existing 'status' field is kept and the new one is ignored.
Attempts:
2 left
💡 Hint
Think about how $addFields modifies documents in aggregation.