Challenge - 5 Problems
MongoDB $project Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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 } }Attempts:
2 left
💡 Hint
Remember that $project reshapes documents and you can rename fields using expressions.
✗ Incorrect
The $project stage includes the
name field and creates a new field mathScore by extracting scores.math. The _id is excluded. So the output shows only name and mathScore with the math score value.📝 Syntax
intermediate2:00remaining
Which $project stage is syntactically correct?
Choose the valid $project stage syntax to include
title and rename author.name to authorName.Attempts:
2 left
💡 Hint
Field paths must be strings starting with $ inside $project.
✗ Incorrect
In $project, to rename a field using a nested field, you must use a string with a $ prefix like "$author.name". Option A correctly uses this syntax. Other options either miss quotes or the $ sign.
🧠 Conceptual
advanced2:00remaining
What does excluding _id in $project do?
In MongoDB aggregation, what is the effect of setting
_id: 0 in a $project stage?Attempts:
2 left
💡 Hint
Think about what
_id normally does in MongoDB documents.✗ Incorrect
By default,
_id is included in all documents. Setting _id: 0 in $project excludes it from the output documents.❓ query_result
advanced2: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 } }Attempts:
2 left
💡 Hint
The $multiply operator calculates the product of the fields.
✗ Incorrect
The $project stage computes a new field
total by multiplying price and quantity. The output includes only total because _id is excluded and other fields are not included.🔧 Debug
expert2:00remaining
Why does this $project stage cause an error?
Consider this $project stage:
Why will this cause an error or unexpected behavior?
{ $project: { name: 1, age: "$age", age: 1 } }Why will this cause an error or unexpected behavior?
Attempts:
2 left
💡 Hint
Check the keys in the object carefully for duplicates.
✗ Incorrect
In JSON and BSON, keys must be unique. Defining
age twice in the same object causes a duplicate key error or the last one overwrites the first, leading to unexpected results.