0
0
MongoDBquery~30 mins

Why advanced stages matter in MongoDB - See It in Action

Choose your learning style9 modes available
Why Advanced Stages Matter in MongoDB Aggregation
📖 Scenario: You are working with a MongoDB database that stores sales data for a retail company. The company wants to analyze their sales to find the total revenue per product category, but only for sales that happened in the last year and where the revenue was above a certain threshold. This requires using advanced aggregation stages to filter, group, and calculate the results efficiently.
🎯 Goal: Build a MongoDB aggregation pipeline that filters sales by date, groups them by product category, calculates total revenue per category, and then filters categories with revenue above a threshold.
📋 What You'll Learn
Create a collection named sales with documents containing product, category, price, quantity, and date fields.
Define a variable minRevenue to set the minimum revenue threshold for filtering categories.
Write an aggregation pipeline that filters sales from the last year, groups by category, calculates total revenue per category, and filters categories with revenue greater than minRevenue.
Add a final stage to sort the results by total revenue in descending order.
💡 Why This Matters
🌍 Real World
Retail companies often analyze sales data to understand which product categories generate the most revenue and to focus marketing or inventory efforts accordingly.
💼 Career
Data analysts and backend developers use MongoDB aggregation pipelines to efficiently process and summarize large datasets for reporting and decision-making.
Progress0 / 4 steps
1
DATA SETUP: Create the sales collection with sample documents
Insert these exact documents into a MongoDB collection called sales: {"product": "Laptop", "category": "Electronics", "price": 1200, "quantity": 5, "date": ISODate("2023-03-15T00:00:00Z")}, {"product": "Headphones", "category": "Electronics", "price": 150, "quantity": 10, "date": ISODate("2023-06-20T00:00:00Z")}, {"product": "Coffee Maker", "category": "Home Appliances", "price": 80, "quantity": 7, "date": ISODate("2022-11-05T00:00:00Z")}, {"product": "Blender", "category": "Home Appliances", "price": 60, "quantity": 3, "date": ISODate("2023-01-10T00:00:00Z")}, {"product": "Notebook", "category": "Stationery", "price": 5, "quantity": 100, "date": ISODate("2023-04-01T00:00:00Z")}
MongoDB
Need a hint?

Use db.sales.insertMany([...]) with the exact documents listed.

2
CONFIGURATION: Define the minimum revenue threshold variable
Create a JavaScript variable called minRevenue and set it to 1000 to use as the minimum revenue filter in the aggregation.
MongoDB
Need a hint?

Use const minRevenue = 1000; to set the threshold.

3
CORE LOGIC: Build the aggregation pipeline with filtering and grouping
Write a MongoDB aggregation pipeline called pipeline that does the following: 1) Filters sales with date greater than or equal to ISODate("2023-01-01T00:00:00Z"), 2) Groups documents by category and calculates total revenue as the sum of price * quantity per category, 3) Filters groups where total revenue is greater than minRevenue.
MongoDB
Need a hint?

Use $match to filter by date, $group to sum revenue, and another $match to filter by minRevenue.

4
COMPLETION: Add sorting stage to the aggregation pipeline
Add a final stage to the pipeline that sorts the results by totalRevenue in descending order.
MongoDB
Need a hint?

Add { $sort: { totalRevenue: -1 } } as the last stage in the pipeline.