Bucket Aggregation : ($bucket and $bucketAuto)
MongoDB gives us some amazing tools to group and analyze data easily. Two such tools are $bucket
and $bucketAuto
— they help us group data into ranges (just like dividing marks into grade categories)
- Introduction to Bucket Aggregation
When we have a lot of data (like marks of students or prices of products), we may want to group them into categories or ranges. For example:
-
0–50 marks = Fail
51–75 = Average
-
76–100 = Excellent
In MongoDB, we can do this using two special tools:
-
$bucket
– We choose the ranges (like 0–50, 51–75…) -
$bucketAuto
– MongoDB automatically creates ranges
- Explanation
Explanation of $bucket
-
You decide where each bucket starts and ends by setting boundary values.
-
If a value doesn’t fall in any bucket, it goes to a default group (if defined).
-
Best when you already know the categories or ranges you want.
Example:
This creates:
-
Bucket 1: 0–50
-
Bucket 2: 51–75
-
Bucket 3: 76–100
Explanation of $bucketAuto
-
MongoDB automatically calculates ranges by dividing the data into equal-sized groups (by document count).
-
You just tell it how many buckets you want.
-
Best when you don’t know the exact ranges, but want balanced grouping.
Example:
MongoDB will divide all values into 3 buckets, each having almost the same number of documents.
- Procedure (Steps to Try)
Let’s try this with a students collection in MongoDB Compass or Shell.
Sample Data: Students Collection
- Screenshots
A) $bucket
– Custom Ranges
Goal: Divide marks into ranges: 0–50, 51–75, 76–100
db.students.aggregate([
{
$bucket: {
groupBy: "$marks",
boundaries: [0, 51, 76, 101], // 0–50, 51–75, 76–100
default: "Out of Range",
output: {
count: { $sum: 1 },
names: { $push: "$name" }
}
}
}
])
B) $bucketAuto
– Automatic Buckets
Goal: Let MongoDB create 3 equal groups based on marks
db.students.aggregate([
{
$bucketAuto: {
groupBy: "$marks",
buckets: 3, // MongoDB will divide into 3 groups
output: {
count: { $sum: 1 },
names: { $push: "$name" }
}
}
}
])
- Future Scope – Why Learn This?
This is super useful for:
-
Making charts and dashboards
-
Creating grade systems or sales ranges
-
Grouping user ages, prices, scores, and more
-
Working with analytics and reports
Later on, you can even connect this with charting tools like MongoDB Charts or Power BI.
- Summary
Feature | $bucket |
$bucketAuto |
---|---|---|
Type | Manual bucket ranges | Automatic bucket ranges |
Use Case | You know the ranges | You don’t know exact ranges |
Example Use | Grades, price categories | Auto grouping for reports |
Mayank Balyan
University: Shree Balaji University, Pune
School: School of Computer Studies
Course: BCA (Bachelor of Computer Applications)
Interests: NoSQL, MongoDB, and related technologies
Acha comment
ReplyDeleteGood work keep it up
ReplyDeleteImformative read!
ReplyDeletewow
ReplyDeleteextremely good
ReplyDeletenicely done
ReplyDeleteSuper insightful
ReplyDeleteNeatly presented!
ReplyDeleteNice work 👍🏻
ReplyDeleteInformative and nice work ..!
ReplyDelete