- Reference >
- Operators >
- Aggregation Pipeline Operators >
- Pipeline Aggregation Stages >
- $bucket (aggregation)
$bucket (aggregation)¶
On this page
Definition¶
-
$bucket¶ New in version 3.4.
Categorizes incoming documents into groups, called buckets, based on a specified expression and bucket boundaries.
Each bucket is represented as a document in the output. The document for each bucket contains an
_idfield, whose value specifies the inclusive lower bound of the bucket and acountfield that contains the number of documents in the bucket. Thecountfield is included by default when theoutputis not specified.$bucketonly produces output documents for buckets that contain at least one input document.Field Type Description groupByexpression An expression to group documents by. To specify a field path, prefix the field name with a dollar sign
$and enclose it in quotes.Unless
$bucketincludes adefaultspecification, each input document must resolve thegroupByfield path or expression to a value that falls within one of the ranges specified by theboundaries.boundariesarray An array of values based on the
groupByexpression that specify the boundaries for each bucket. Each adjacent pair of values acts as the inclusive lower boundary and the exclusive upper boundary for the bucket. You must specify at least two boundaries.The specified values must be in ascending order and all of the same type. The exception is if the values are of mixed numeric types, such as:
[ 10, NumberLong(20), NumberInt(30) ]If the values are documents, they must be wrapped in the
$literaloperator.Example
An array of
[ 0, 5, 10 ]creates two buckets:- [0, 5) with inclusive lower bound
0and exclusive upper bound5. - [5, 10) with inclusive lower bound
5and exclusive upper bound10.
defaultliteral Optional. A literal that specifies the
_idof an additional bucket that contains all documents whosegroupByexpression result does not fall into a bucket specified byboundaries.If unspecified, each input document must resolve the
groupByexpression to a value within one of the bucket ranges specified byboundariesor the operation throws an error.The
defaultvalue must be less than the lowestboundariesvalue, or greater than or equal to the highestboundariesvalue.The
defaultvalue can be of a different type than the entries inboundaries.outputdocument Optional. A document that specifies the fields to include in the output documents in addition to the
_idfield. To specify the field to include, you must use accumulator expressions.The default
countfield is not included in the output document whenoutputis specified. Explicitly specify thecountexpression as part of the output document to include it:- [0, 5) with inclusive lower bound
Behavior¶
$bucket requires at least one of the following conditions to be met
or the operation throws an error:
- Each input document resolves the
groupByexpression to a value within one of the bucket ranges specified byboundaries, or - A
defaultvalue is specified to bucket documents whosegroupByvalues are outside of theboundariesor of a different BSON type than the values inboundaries.
If the groupBy expression resolves to an array or a document,
$bucket arranges the input documents into buckets using the
comparison logic from $sort.
Example¶
Consider a collection artwork with the following documents:
The following operation uses the $bucket aggregation stage to
arrange the artwork collection into buckets according to price:
The buckets have the following boundaries:
- [0, 200) with inclusive lowerbound
0and exclusive upper bound200. - [200, 400) with inclusive lowerbound
200and exclusive upper bound400. - “Other” is the
defaultbucket for documents without prices or prices outside the ranges above.
The operation returns the following documents:
Using $bucket with $facet¶
The $bucket stage can be used within the $facet
stage to process multiple aggregation pipelines on artwork in a
single aggregation stage.
The following operation groups the documents from artwork into
buckets based on price and year:
The first facet groups the input documents by price. The
buckets have the following boundaries:
- [0, 200) with inclusive lowerbound
0and exclusive upper bound200. - [200, 400) with inclusive lowerbound
200and exclusive upper bound400. - “Other”, the``default`` bucket containing documents without prices or prices outside the ranges above.
The second facet groups the input documents by year. The buckets
have the following boundaries:
- [1890, 1910) with inclusive lowerbound
1890and exclusive upper bound1910. - [1910, 1920) with inclusive lowerbound
1910and exclusive upper bound1920. - [1920, 1940) with inclusive lowerbound
1910and exclusive upper bound1940. - “Unknown”, the``default`` bucket containing documents without years or years outside the ranges above.
The operation returns the following document:
See also $bucketAuto