0
0
MongoDBquery~10 mins

$bucket and $bucketAuto for distribution in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - $bucket and $bucketAuto for distribution
Start with data array
$bucket or $bucketAuto stage
Define boundaries or bucket count
Group data into buckets
Count documents per bucket
Output buckets with counts
Data flows into a bucket stage where it is grouped into ranges or automatic buckets, then counts per bucket are output.
Execution Sample
MongoDB
db.sales.aggregate([
  {$bucket: {
    groupBy: "$price",
    boundaries: [0, 50, 100, 200],
    default: "Other",
    output: {count: {$sum: 1}}
  }}
])
Groups sales documents by price ranges 0-50, 50-100, 100-200, counting how many fall in each.
Execution Table
StepDocument priceBucket boundariesBucket assignedCount update
123[0,50,100,200]0-50count=1
275[0,50,100,200]50-100count=1
3150[0,50,100,200]100-200count=1
445[0,50,100,200]0-50count=2
5210[0,50,100,200]Othercount=1
690[0,50,100,200]50-100count=2
7180[0,50,100,200]100-200count=2
ExitN/AN/AN/AAll documents processed
💡 All documents processed and assigned to buckets or default.
Variable Tracker
BucketStartAfter 1After 2After 3After 4After 5After 6After 7Final
0-50011122222
50-100001111222
100-200000111122
Other000001111
Key Moments - 3 Insights
Why does the document with price 210 go to 'Other' bucket?
Because 210 is outside the defined boundaries [0,50,100,200], so it is assigned to the 'default' bucket 'Other' as shown in execution_table step 5.
How does $bucket decide which bucket a document belongs to?
$bucket compares the document's value to the boundaries and assigns it to the bucket where value >= lower boundary and < upper boundary, as seen in steps 1-7.
What happens if a document's value equals a boundary?
It belongs to the bucket starting at that boundary (inclusive), for example price 50 would go to bucket 50-100.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the count for bucket '50-100' after step 6?
A2
B1
C3
D0
💡 Hint
Check the 'count update' column for step 6 and the variable_tracker row for '50-100' after step 6.
At which step does the 'Other' bucket first get a count?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look at execution_table row where 'Bucket assigned' is 'Other' and see the count update.
If the boundaries changed to [0, 100, 200], how would the bucket for price 75 change?
AIt would be in 100-200 bucket
BIt would be in Other bucket
CIt would be in 0-100 bucket
DIt would not be assigned
💡 Hint
Check how boundaries define buckets and where 75 fits between 0 and 100.
Concept Snapshot
$bucket groups data by defined boundaries.
Values >= lower and < upper boundary go to that bucket.
Documents outside boundaries go to 'default' bucket.
$bucketAuto creates equal-sized buckets automatically.
Both count documents per bucket.
Full Transcript
This visual execution shows how MongoDB's $bucket stage groups documents by ranges. Each document's value is compared to boundaries. If it fits between two boundaries, it goes to that bucket. If not, it goes to the default bucket. Counts per bucket increase as documents are processed. This helps understand how data distribution works in aggregation.