📖 Scenario: You work at a small bookstore that keeps track of sales in a MongoDB collection. Each sale document records the book title and the number of copies sold. You want to find the minimum and maximum number of copies sold among all sales to understand the range of sales performance.
🎯 Goal: Build a MongoDB aggregation pipeline that uses the $min and $max accumulators to find the smallest and largest number of copies sold in the sales collection.
📋 What You'll Learn
Create a sales collection with documents containing title and copies_sold fields.
Define a variable pipeline that holds the aggregation pipeline array.
Use the $group stage with $min and $max accumulators on the copies_sold field.
Complete the pipeline so it returns a single document with minCopies and maxCopies fields.
💡 Why This Matters
🌍 Real World
Bookstores and many businesses use MongoDB to analyze sales data and find minimum and maximum values to understand performance ranges.
💼 Career
Knowing how to use MongoDB aggregation with accumulators like $min and $max is important for data analysts and backend developers working with NoSQL databases.
Progress0 / 4 steps
1
Create the sales collection data
Create a variable called sales that is an array of documents with these exact entries: { title: "Book A", copies_sold: 5 }, { title: "Book B", copies_sold: 12 }, { title: "Book C", copies_sold: 7 }, { title: "Book D", copies_sold: 3 }, and { title: "Book E", copies_sold: 10 }.
MongoDB
Hint
Use an array of objects with the exact field names and values given.
2
Define the aggregation pipeline variable
Create a variable called pipeline and set it to an empty array [].
MongoDB
Hint
Just create an empty array named pipeline for now.
3
Add the $group stage with $min and $max accumulators
Add a $group stage object to the pipeline array. Use _id: null and add minCopies: { $min: "$copies_sold" } and maxCopies: { $max: "$copies_sold" } fields inside the $group stage.
MongoDB
Hint
Remember to use $group with _id: null to aggregate all documents together.
4
Complete the aggregation pipeline setup
Add a comment above the pipeline variable that says // Aggregation pipeline to find min and max copies sold.
MongoDB
Hint
Adding a comment helps explain what the pipeline does.
Practice
(1/5)
1. What do the $min and $max accumulators do in a MongoDB aggregation pipeline?
easy
A. $min and $max sort documents in ascending and descending order respectively.
B. $min and $max both find the average value in a group.
C. $min finds the first document and $max finds the last document in a group.
D. $min finds the smallest value and $max finds the largest value in a group.
Solution
Step 1: Understand the purpose of $min and $max
$min returns the smallest value from the grouped documents, while $max returns the largest value.
Step 2: Compare with other options
The other options describe different operations like average, first/last document, or sorting, which are not what $min and $max do.
Final Answer:
$min finds the smallest value and $max finds the largest value in a group. -> Option D
Quick Check:
$min = smallest, $max = largest [OK]
Hint: Remember: min = smallest, max = largest value [OK]
Common Mistakes:
Confusing $min and $max with average functions
Thinking they return documents instead of values
Mixing accumulators with sorting operators
2. Which of the following is the correct syntax to use $max in a $group stage to find the highest score from a field named score?
A. Field name 'price' in $min is missing quotes and $ prefix.
B. Using $max and $min together is not allowed.
C. The _id field cannot be a field name.
D. The pipeline is correct and has no errors.
Solution
Step 1: Check syntax of $min and $max usage
The $min accumulator uses price without quotes and $ prefix, which is incorrect. It should be "$price".
Step 2: Verify other parts of the pipeline
$max is correctly used with "$price". Grouping by "$category" is valid. Using both accumulators together is allowed.
Final Answer:
Field name 'price' in $min is missing quotes and $ prefix. -> Option A
Quick Check:
Field names need "$" prefix inside accumulators [OK]
Hint: Always quote and prefix field names with $ inside accumulators [OK]
Common Mistakes:
Omitting $ before field names inside accumulators
Thinking $min and $max cannot be used together
Misunderstanding _id field usage in $group
5. You have a collection of sales documents with fields region and amount. You want to find the smallest and largest sale amounts per region, but only for regions where the smallest sale is above 100. Which aggregation pipeline correctly achieves this?