📖 Scenario: You work for a small online bookstore. You have a collection of books documents in MongoDB. Each book has fields like title, author, price, and copies_sold. You want to analyze sales data using aggregation pipelines.
🎯 Goal: Build a MongoDB aggregation pipeline step-by-step that uses expressions to calculate total revenue per book and filter books with revenue above a certain amount.
📋 What You'll Learn
Create a books collection with exact documents
Add a variable for minimum revenue threshold
Use $addFields with an expression to calculate revenue
Use $match to filter books by revenue
💡 Why This Matters
🌍 Real World
Online stores and data analysts use aggregation pipelines to calculate sales metrics and filter products dynamically.
💼 Career
Understanding expressions in MongoDB pipelines is essential for backend developers and data engineers working with NoSQL databases.
Progress0 / 4 steps
1
DATA SETUP: Create the books collection with documents
Create a variable called books that is a list of three documents exactly as follows: {"title": "Learn MongoDB", "author": "Alice", "price": 30, "copies_sold": 100}, {"title": "Advanced MongoDB", "author": "Bob", "price": 45, "copies_sold": 50}, and {"title": "MongoDB for Beginners", "author": "Carol", "price": 25, "copies_sold": 200}.
MongoDB
Hint
Use a Python list with dictionaries for each book document.
2
CONFIGURATION: Set a minimum revenue threshold
Create a variable called min_revenue and set it to 3000.
MongoDB
Hint
Just assign the number 3000 to min_revenue.
3
CORE LOGIC: Use $addFields to calculate revenue per book
Create a variable called pipeline that is a list with one stage: { "$addFields": { "revenue": { "$multiply": ["$price", "$copies_sold"] } }}.
MongoDB
Hint
Use $multiply inside $addFields to create a new field revenue.
4
COMPLETION: Add a $match stage to filter books by revenue
Add a second stage to the pipeline list: { "$match": { "revenue": { "$gt": min_revenue } } }.
MongoDB
Hint
Add a $match stage after $addFields to filter books with revenue greater than min_revenue.
Practice
(1/5)
1. What is the main reason expressions are important in MongoDB aggregation pipelines?
easy
A. They backup the database during aggregation.
B. They create new collections automatically.
C. They replace the need for indexes.
D. They allow you to calculate and transform data within pipeline stages.
Solution
Step 1: Understand the role of expressions in pipelines
Expressions let you perform calculations and data transformations inside stages like $project and $group.
Step 2: Compare other options
Options A, B, and C describe unrelated features not handled by expressions.
Final Answer:
They allow you to calculate and transform data within pipeline stages. -> Option D
Quick Check:
Expressions = Data transformation [OK]
Hint: Expressions transform data inside stages like $project [OK]
Common Mistakes:
Thinking expressions create collections
Confusing expressions with indexing
Assuming expressions handle backups
2. Which of the following is the correct syntax to use an expression in a $project stage to add two fields price and tax?
B. Field names should not have $ prefix inside $add.
C. The $add operator requires an array of expressions, not separate arguments.
D. The syntax is correct; no error.
Solution
Step 1: Review $add operator syntax
$add expects a single array argument containing expressions, not multiple separate arguments.
Step 2: Analyze given code
The code passes "$price" and "$tax" as separate arguments instead of inside an array.
Final Answer:
The $add operator requires an array of expressions, not separate arguments. -> Option C
Quick Check:
$add needs array input [OK]
Hint: Always pass array to $add, not separate arguments [OK]
Common Mistakes:
Passing multiple arguments instead of array
Removing $ from field names inside expressions
Assuming $project can't use expressions
5. You want to create a pipeline that groups sales by product and calculates the average revenue per sale, but only for sales where quantity is greater than 5. Which expression usage is correct inside the pipeline?