📖 Scenario: You work at a small bookstore that keeps its inventory in a MongoDB collection. Each book document has details like title, author, price, and stock count.You want to create a report that only shows the book title and price, hiding other details.
🎯 Goal: Build a MongoDB aggregation pipeline using the $project stage to shape the output documents to include only the title and price fields.
📋 What You'll Learn
Create a collection named books with 3 book documents having fields title, author, price, and stock.
Define a variable projection that specifies the $project stage to include only title and price.
Write an aggregation query on books using the projection variable.
Complete the pipeline so the output documents only show title and price.
💡 Why This Matters
🌍 Real World
Shaping output with $project is common when you want to show only relevant data to users or reports, hiding sensitive or unnecessary fields.
💼 Career
Many jobs require working with MongoDB aggregations to optimize data retrieval and presentation, making $project a key skill.
Progress0 / 4 steps
1
Create the books collection with 3 book documents
Create a variable called books and assign it an array with these exact 3 documents: { title: "The Hobbit", author: "J.R.R. Tolkien", price: 15, stock: 5 }, { title: "1984", author: "George Orwell", price: 12, stock: 8 }, { title: "To Kill a Mockingbird", author: "Harper Lee", price: 10, stock: 3 }
MongoDB
Hint
Use an array of objects with the exact field names and values.
2
Define the projection variable for the $project stage
Create a variable called projection and assign it an object with a $project field that includes only title and price set to 1.
MongoDB
Hint
Use $project with title: 1 and price: 1 to include only those fields.
3
Write the aggregation query using the projection variable
Create a variable called result and assign it the result of calling books.aggregate with an array containing the projection variable.
MongoDB
Hint
Use books.aggregate with an array containing projection.
4
Complete the pipeline to output only title and price
Add a final line to the code that assigns resultArray to the array form of result by calling toArray().
MongoDB
Hint
Call toArray() on result to get the final array.
Practice
(1/5)
1. What does the $project stage do in a MongoDB aggregation pipeline?
easy
A. It filters documents based on conditions.
B. It shapes the output by selecting, renaming, or creating fields.
C. It sorts the documents in ascending or descending order.
D. It groups documents by a specified key.
Solution
Step 1: Understand the purpose of $project
The $project stage is used to control which fields appear in the output documents and how they are shaped.
Step 2: Compare with other stages
Filtering is done by $match, sorting by $sort, and grouping by $group. So $project is for shaping output fields.
Final Answer:
It shapes the output by selecting, renaming, or creating fields. -> Option B
Quick Check:
$project shapes output = A [OK]
Hint: Remember: $project controls output fields only [OK]
Common Mistakes:
Confusing $project with $match for filtering
Thinking $project sorts documents
Mixing $project with $group for aggregation
2. Which of the following is the correct syntax to include only the name and age fields in the output using $project?
easy
A. { $project: { name: 1, age: 1 } }
B. { $project: { name: 0, age: 0 } }
C. { $project: { name: true, age: true } }
D. { $project: { name: 'include', age: 'include' } }
Solution
Step 1: Recall $project syntax for inclusion
To include fields, set them to 1. To exclude, set to 0. Boolean or strings are invalid.
C. Field name "age" should be prefixed with $ to reference the field.
D. The field fullName cannot be renamed.
Solution
Step 1: Check field references in expressions
In MongoDB aggregation, to reference a field inside expressions, prefix with $. Here, "age" is missing $.
Step 2: Validate other parts
$multiply is valid in $project. Excluding _id is allowed. Renaming fields is allowed.
Final Answer:
Field name "age" should be prefixed with $ to reference the field. -> Option C
Quick Check:
Field references need $ prefix [OK]
Hint: Always prefix field names with $ inside expressions [OK]
Common Mistakes:
Omitting $ before field names in expressions
Thinking $multiply is invalid in $project
Believing _id cannot be excluded
5. You have documents with fields firstName, lastName, and birthYear. Write a $project stage that outputs fullName as concatenation of first and last names separated by a space, and age calculated as 2024 minus birthYear. Which is the correct stage?