📖 Scenario: You work for a small online bookstore. You want to keep track of the total number of books sold per author to quickly show popular authors without counting every sale each time.
🎯 Goal: Build a MongoDB collection that stores book sales and a pre-aggregated collection that keeps the total sales per author updated using computed fields.
📋 What You'll Learn
Create a sales collection with documents containing book, author, and copies_sold fields.
Create a variable min_copies to filter sales with at least 5 copies sold.
Write an aggregation pipeline that sums copies_sold per author only for sales meeting the min_copies threshold.
Create a pre_aggregated_sales collection that stores the total copies sold per author.
💡 Why This Matters
🌍 Real World
Pre-aggregation helps websites and apps show summary data quickly without recalculating totals every time a user requests it.
💼 Career
Database developers and data engineers often create pre-aggregated collections or tables to improve performance and user experience.
Progress0 / 4 steps
1
DATA SETUP: Create the sales collection with sample sales data
Use db.sales.insertMany() with an array of documents.
2
CONFIGURATION: Define a variable min_copies to filter sales
Create a variable called min_copies and set it to 5. This will be used to filter sales with at least 5 copies sold.
MongoDB
Hint
Use const min_copies = 5 to create the variable.
3
CORE LOGIC: Aggregate total copies sold per author with filtering
Write an aggregation pipeline on sales that filters documents where copies_sold is greater than or equal to min_copies, then groups by author and sums copies_sold as total_copies. Store the result in a variable called authorSales.
MongoDB
Hint
Use $match to filter and $group to sum copies sold per author.
4
COMPLETION: Store the pre-aggregated results in pre_aggregated_sales collection
Insert the documents from authorSales into a new collection called pre_aggregated_sales.
MongoDB
Hint
Use db.pre_aggregated_sales.insertMany(authorSales) to save the results.
Practice
(1/5)
1. What is the main purpose of using a computed pattern for pre-aggregation in MongoDB?
easy
A. To delete all raw data after aggregation
B. To create indexes on every field automatically
C. To calculate and store summary data ahead of time for faster queries
D. To replicate data across multiple servers
Solution
Step 1: Understand pre-aggregation concept
Pre-aggregation means computing summary data before queries run, so queries are faster.
Step 2: Identify the main goal
The goal is to store computed summaries to avoid recalculating on every query.
Final Answer:
To calculate and store summary data ahead of time for faster queries -> Option C
Quick Check:
Pre-aggregation = faster queries [OK]
Hint: Pre-aggregation means compute summaries before queries [OK]
Common Mistakes:
Confusing pre-aggregation with data deletion
Thinking it automatically creates indexes
Mixing it with data replication
2. Which MongoDB aggregation stage is primarily used to compute grouped summary data for pre-aggregation?
easy
A. $match
B. $group
C. $project
D. $sort
Solution
Step 1: Recall aggregation stages
$group is used to group documents and compute summary values like sums or counts.
Step 2: Identify correct stage for pre-aggregation
Pre-aggregation requires grouping and computing, so $group is the right stage.
Final Answer:
$group -> Option B
Quick Check:
$group = grouping and computing summaries [OK]
Hint: Use $group to compute grouped summaries [OK]
Common Mistakes:
Choosing $match which filters but does not compute
Confusing $project with grouping
Selecting $sort which only orders data
3. Given the following aggregation pipeline, what will be the output?
$sum requires a field path starting with $, but "amount" is missing the $.
Step 2: Confirm correct syntax
It should be $sum: "$amount" to sum the amount field values.
Final Answer:
Missing $ before amount in $sum operator -> Option A
Quick Check:
$sum needs $field syntax [OK]
Hint: Field names in aggregation need $ prefix [OK]
Common Mistakes:
Forgetting $ before field names in operators
Assuming $match is mandatory before $group
Misusing _id field in $group
5. You want to pre-aggregate sales data by month and product category, storing total sales and average price. Which aggregation pipeline correctly computes these values?