📖 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
1
Create the sales data collection
Create a variable called sales that is a list of documents with these exact entries: { bookTitle: "The Hobbit", author: "J.R.R. Tolkien", copiesSold: 5 }, { bookTitle: "1984", author: "George Orwell", copiesSold: 8 }, { bookTitle: "The Hobbit", author: "J.R.R. Tolkien", copiesSold: 3 }, { bookTitle: "Dune", author: "Frank Herbert", copiesSold: 7 }.
MongoDB
Hint
Use a list of dictionaries with the exact keys and values as shown.
2
Define the aggregation pipeline variable
Create a variable called pipeline and set it to an empty list to hold the aggregation stages.
MongoDB
Hint
Just create an empty list named pipeline.
3
Add the $group stage to sum copies sold by bookTitle
Add a $group stage to the pipeline list. Group by _id set to $bookTitle and create a field totalCopies that sums copiesSold.
MongoDB
Hint
Use { "$group": { "_id": "$bookTitle", "totalCopies": { "$sum": "$copiesSold" } } } inside the pipeline list.
4
Add a $sort stage to order results by totalCopies descending
Add a $sort stage to the pipeline list that sorts by totalCopies in descending order (-1).
MongoDB
Hint
Add { "$sort": { "totalCopies": -1 } } as the last stage in the pipeline list.
Practice
(1/5)
1. What is the main purpose of the $group stage in a MongoDB aggregation pipeline?
easy
A. To project specific fields from documents
B. To filter documents based on a condition
C. To sort documents in ascending or descending order
D. To group documents by a specified key and perform calculations on each group
Solution
Step 1: Understand the role of $group
The $group stage collects documents into groups based on a key defined in the _id field.
Step 2: Identify what $group does with groups
It allows performing calculations like sum, count, or average on each group.
Final Answer:
To group documents by a specified key and perform calculations on each group -> Option D
Quick Check:
$group groups and calculates [OK]
Hint: Remember: $group always groups and aggregates [OK]
Common Mistakes:
Confusing $group with $match for filtering
Thinking $group sorts documents
Assuming $group projects fields without grouping
2. Which of the following is the correct syntax to group documents by the field category and count the number of documents in each group?
The _id field is mandatory to specify the grouping key.
Step 2: Verify other syntax elements
The $sum operator and field references are correct; only _id is missing.
Final Answer:
Missing _id field for grouping key -> Option C
Quick Check:
_id is required in $group [OK]
Hint: Always include _id in $group stage [OK]
Common Mistakes:
Omitting the _id field
Confusing field names with grouping key
Thinking $sum needs special syntax
5. You have a collection of sales documents with fields store, item, and quantity. You want to find the total quantity sold per store, but only for stores where the total quantity exceeds 100. Which aggregation pipeline correctly achieves this?