$bucket and $bucketAuto for distribution in MongoDB
📖 Scenario: You work for a bookstore that wants to analyze how many books fall into different price ranges. You have a collection of books with their prices, and you want to group these books into price buckets to understand the distribution.
🎯 Goal: Build two MongoDB aggregation queries: one using $bucket to manually define price ranges, and another using $bucketAuto to automatically create price buckets. This will help the bookstore see how books are distributed by price.
📋 What You'll Learn
Create a collection called books with documents containing title and price fields.
Write an aggregation query using $bucket to group books into these price ranges: 0-10, 10-20, 20-30, and 30 and above.
Write an aggregation query using $bucketAuto to automatically create 4 buckets based on book prices.
Use count to see how many books fall into each bucket.
💡 Why This Matters
🌍 Real World
Bookstores and retailers often want to understand how products are priced and distributed across price ranges to make pricing and marketing decisions.
💼 Career
Data analysts and database developers use $bucket and $bucketAuto in MongoDB to group and summarize data efficiently for reports and dashboards.
Use db.books.insertMany([...]) with the exact documents listed.
2
Define the price boundaries for manual bucketing
Create a variable called priceBoundaries and set it to the array [0, 10, 20, 30, 1000] to define the price ranges for bucketing.
MongoDB
Hint
Define priceBoundaries as an array with the exact values.
3
Write the aggregation query using $bucket
Write an aggregation query on books using $bucket with groupBy set to $price, boundaries set to priceBoundaries, and default set to 'Other'. Include a count field that sums 1 for each book in the bucket.
Write an aggregation query on books using $bucketAuto with groupBy set to $price and buckets set to 4. Include a count field that sums 1 for each book in the bucket.
Hint: Remember: groupBy and boundaries are mandatory for $bucket [OK]
Common Mistakes:
Using field instead of groupBy
Confusing $bucket with $bucketAuto syntax
Omitting required boundaries array
3. Given the collection documents: [{score: 10}, {score: 20}, {score: 55}, {score: 70}, {score: 90}], what will be the output buckets using this aggregation?
The error says: "Boundaries must be sorted in ascending order and unique." What is the likely cause?
medium
A. Boundaries array is missing the lowest value 0.
B. Boundaries array is not sorted or has duplicates.
C. The default value must be a number, not a string.
D. The groupBy field must be an integer, not a string.
Solution
Step 1: Understand error message
The error says boundaries must be sorted ascending and unique, so check boundaries array.
Step 2: Check boundaries array
If boundaries are not sorted or have duplicates, error occurs. The example boundaries [20, 30, 40] are sorted and unique, so likely in actual code they are not.
Final Answer:
Boundaries array is not sorted or has duplicates. -> Option B
Quick Check:
Boundaries must be sorted unique = B [OK]
Hint: Check boundaries array order and duplicates first [OK]
Common Mistakes:
Ignoring error message details
Thinking default value type causes this error
Assuming groupBy field type causes this error
5. You want to create 4 buckets automatically for a collection with a wide range of numeric values in field price. Which aggregation stage correctly achieves this using $bucketAuto?