0
0
MongoDBquery~20 mins

$project stage for shaping output in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $project 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 $project stage?
Given the collection students with documents like { name: "Alice", scores: { math: 90, english: 85 }, age: 20 }, what will be the output of this aggregation pipeline stage?
{ $project: { name: 1, mathScore: "$scores.math", _id: 0 } }
A[{ "name": "Alice", "scores": { "math": 90, "english": 85 } }]
B[{ "name": "Alice", "mathScore": 90 }]
C[{ "name": "Alice", "mathScore": { "$scores.math": 90 } }]
D[{ "name": "Alice", "mathScore": "$scores.math" }]
Attempts:
2 left
💡 Hint
Remember that $project reshapes documents and you can rename fields using expressions.
📝 Syntax
intermediate
2:00remaining
Which $project stage is syntactically correct?
Choose the valid $project stage syntax to include title and rename author.name to authorName.
A{ $project: { title: 1, authorName: "$author.name" } }
B{ $project: { title: 1, authorName: author.name } }
C{ $project: { title: 1, authorName: "author.name" } }
D{ $project: { title: 1, authorName: $author.name } }
Attempts:
2 left
💡 Hint
Field paths must be strings starting with $ inside $project.
🧠 Conceptual
advanced
2:00remaining
What does excluding _id in $project do?
In MongoDB aggregation, what is the effect of setting _id: 0 in a $project stage?
AIt causes a syntax error because <code>_id</code> cannot be excluded.
BIt renames the <code>_id</code> field to zero.
CIt includes the <code>_id</code> field with value zero.
DIt removes the <code>_id</code> field from the output documents.
Attempts:
2 left
💡 Hint
Think about what _id normally does in MongoDB documents.
query_result
advanced
2:00remaining
What is the output of this $project with computed field?
Given documents { price: 100, quantity: 3 }, what is the output of this $project stage?
{ $project: { total: { $multiply: ["$price", "$quantity"] }, _id: 0 } }
A[{ "total": 300 }]
B[{ "total": "$price * $quantity" }]
C[{ "total": { "$multiply": [100, 3] } }]
D[{ "price": 100, "quantity": 3, "total": 300 }]
Attempts:
2 left
💡 Hint
The $multiply operator calculates the product of the fields.
🔧 Debug
expert
2:00remaining
Why does this $project stage cause an error?
Consider this $project stage:
{ $project: { name: 1, age: "$age", age: 1 } }

Why will this cause an error or unexpected behavior?
ABecause <code>_id</code> is missing and must always be included.
BBecause <code>age: "$age"</code> is invalid syntax in $project.
CBecause the field <code>age</code> is defined twice, causing a duplicate key error.
DBecause <code>name: 1</code> must come after <code>age</code> fields.
Attempts:
2 left
💡 Hint
Check the keys in the object carefully for duplicates.