0
0
MongoDBquery~30 mins

Pipeline mental model (stages flow) in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Build a MongoDB Aggregation Pipeline
📖 Scenario: You work at a small online bookstore. You have a collection of books with details like title, author, genre, and sales. You want to analyze the data to find the total sales per genre.
🎯 Goal: Create a MongoDB aggregation pipeline that groups books by genre and calculates the total sales for each genre.
📋 What You'll Learn
Create a books collection with specific book documents
Add a variable for the minimum sales threshold
Build an aggregation pipeline that filters books with sales above the threshold
Group the filtered books by genre and sum their sales
💡 Why This Matters
🌍 Real World
Aggregation pipelines help analyze and summarize large collections of data in MongoDB, such as sales reports or user activity.
💼 Career
Understanding aggregation pipelines is essential for roles like data analyst, backend developer, or database administrator working with MongoDB.
Progress0 / 4 steps
1
DATA SETUP: Create the books collection with documents
Create a variable called books that is a list of documents with these exact entries: { title: 'The Alchemist', author: 'Paulo Coelho', genre: 'Fiction', sales: 500 }, { title: 'Clean Code', author: 'Robert C. Martin', genre: 'Programming', sales: 300 }, { title: 'The Pragmatic Programmer', author: 'Andrew Hunt', genre: 'Programming', sales: 250 }, { title: '1984', author: 'George Orwell', genre: 'Fiction', sales: 400 }, and { title: 'Deep Work', author: 'Cal Newport', genre: 'Self-help', sales: 150 }.
MongoDB
Need a hint?

Use a Python list of dictionaries to represent the books collection documents.

2
CONFIGURATION: Set a sales threshold variable
Create a variable called min_sales and set it to 200 to filter books with sales greater than or equal to this value.
MongoDB
Need a hint?

Just create a variable min_sales and assign the number 200.

3
CORE LOGIC: Build the aggregation pipeline stages
Create a variable called pipeline that is a list of two stages: first, a $match stage that filters books with sales greater than or equal to min_sales, and second, a $group stage that groups by genre and calculates the total sales as totalSales.
MongoDB
Need a hint?

Use a list with two dictionaries: one for $match filtering sales, one for $group grouping by genre and summing sales.

4
COMPLETION: Prepare the final aggregation command
Create a variable called aggregation_command that is a dictionary with the key aggregate set to 'books' and the key pipeline set to the pipeline variable.
MongoDB
Need a hint?

Create a dictionary with keys aggregate and pipeline using the variable pipeline.