Buckets: upper boundaries

I have a question about upper boundaries of buckets (upper bound of the last bucket)

Doc describes that min is inclusive and max is exclusive.

. If we have a series with 1000 elements (_id from 1 to 1000) don’t last bucket should have a value for max of 1001, or a count of 199:

for (i=1;i<=1000; i++) {db.series.insert({"_id": i})}

db.series.aggregate([{bucketAuto: { groupBy: "_id",
buckets: 5
}}])

Result:

{ “_id” : { “min” : 1, “max” : 201 }, “count” : 200 }
{ “_id” : { “min” : 201, “max” : 401 }, “count” : 200 }
{ “_id” : { “min” : 401, “max” : 601 }, “count” : 200 }
{ “_id” : { “min” : 601, “max” : 801 }, “count” : 200 }
{ “_id” : { “min” : 801, “max” : 1000 }, “count” : 200 }

Regards,
Antonio

Hi,

I believe $bucketAuto determines bucket boundaries automatically, trying to distribute the documents evenly if you do not specify a granularity. Please see these examples:

https://docs.mongodb.com/manual/reference/operator/aggregation/bucketAuto/#comparing-different-granularities

Hope it helps.

José Carlos

Thanks José Carlos,

I believe being automatically determined it takes the max value of collection.

If I use “manual bucket” it’s ok.

db.series.aggregate([{bucket: {groupBy: "_id",
boundaries: [ 1, 201, 401, 601, 801, 1000 ],
default: “outOfRange”}}])

{ “_id” : 1, “count” : 200 }
{ “_id” : 201, “count” : 200 }
{ “_id” : 401, “count” : 200 }
{ “_id” : 601, “count” : 200 }
{ “_id” : 801, “count” : 199 }
{ “_id” : “outOfRange”, “count” : 1 }

.
Last bucket is from 801 to 999 (inluded) and then, element _id=1000 is included at “outOfRange” group in my example.

Thanks a lot,

Antonio

1 Like

You are welcome! Thanks for sharing your example!

José Carlos