$project stage for shaping output
📖 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