$group Stage for Aggregation in MongoDB
📖 Scenario: You work at a bookstore that keeps sales records in a MongoDB collection. Each sale document records the bookTitle, author, and copiesSold. You want to find out how many copies of each book were sold in total.
🎯 Goal: Build a MongoDB aggregation pipeline using the $group stage to calculate the total copies sold for each book.
📋 What You'll Learn
Create a collection named
sales with documents containing bookTitle, author, and copiesSold fields.Define a variable
pipeline that holds the aggregation stages.Use the
$group stage to group documents by bookTitle and calculate the total copiesSold for each book.Add a final stage to sort the results by total copies sold in descending order.
💡 Why This Matters
🌍 Real World
Bookstores and many businesses use MongoDB aggregation pipelines to analyze sales data and generate reports.
💼 Career
Understanding the <code>$group</code> stage is essential for data analysts and backend developers working with MongoDB to summarize and aggregate data efficiently.
Progress0 / 4 steps