- Reference >
- Operators >
- Aggregation Pipeline 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 and outputs a document per each bucket. Each output document contains an
_idfield whose value specifies the inclusive lower bound of the bucket. The output option specifies the fields included in each output document.$bucketonly produces output documents for buckets that contain at least one input document.
Syntax¶
The $bucket document contains the following fields:
| Field | Type | Description |
|---|---|---|
| groupBy | expression | An expression to group
documents by. To specify a field path, prefix the field name with a
dollar sign Unless |
| boundaries | array | An array of values based on the groupBy expression 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:
Example An array of
|
| default | literal | Optional. A literal that specifies the If unspecified, each input document must resolve the
The The |
| output | document | Optional. A document that specifies the fields to include in
the output documents in addition to the If you do not specify an If you specify an |
Behavior¶
$bucket requires at least one of the following conditions to be met
or the operation throws an error:
- Each input document resolves the groupBy expression to a value within one of the bucket ranges specified by boundaries, or
- A default value is specified to bucket
documents whose
groupByvalues 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.
Examples¶
Bucket by Year and Filter by Bucket Results¶
From the mongo shell, create a sample collection named
artists with the following documents:
The following operation groups the documents into buckets
according to the year_born field and filters based on the count
of documents in the buckets:
- First Stage
The
$bucketstage groups the documents into buckets by theyear_bornfield. The buckets have the following boundaries:- [1840, 1850) with inclusive lowerbound
1840and exclusive upper bound1850. - [1850, 1860) with inclusive lowerbound
1850and exclusive upper bound1860. - [1860, 1870) with inclusive lowerbound
1860and exclusive upper bound1870. - [1870, 1880) with inclusive lowerbound
1870and exclusive upper bound1880. - If a document did not contain the
year_bornfield or itsyear_bornfield was outside the ranges above, it would be placed in the default bucket with the_idvalue"Other".
The stage includes the output document to determine the fields to return:
_idInclusive lower bound of the bucket. countCount of documents in the bucket. artistsArray of documents containing information on each artist in the bucket. Each document contains the artist’s
name, which is a concatenation (i.e.$concat) of the artist’sfirst_nameandlast_name.year_born
This stage passes the following documents to the next stage:
- [1840, 1850) with inclusive lowerbound
- Second Stage
The
$matchstage filters the output from the previous stage to only return buckets which contain more than 3 documents.The operation returns the following document:
Use $bucket with $facet to Bucket by Multiple Fields¶
You can use the $facet stage to perform multiple
$bucket aggregations in a single stage.
From the mongo shell, create a sample collection named
artwork with the following documents:
The following operation uses two $bucket stages within a
$facet stage to create two groupings, one by price and
the other by year:
- First Facet
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
defaultbucket containing documents without prices or prices outside the ranges above.
The
$bucketstage includes the output document to determine the fields to return:_idInclusive lower bound of the bucket. countCount of documents in the bucket. artworkArray of documents containing information on each artwork in the bucket. averagePriceEmploys the $avgoperator to display the average price of all artwork in the bucket.- [0, 200) with inclusive lowerbound
- Second Facet
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
defaultbucket containing documents without years or years outside the ranges above.
The
$bucketstage includes the output document to determine the fields to return:countCount of documents in the bucket. artworkArray of documents containing information on each artwork in the bucket. - [1890, 1910) with inclusive lowerbound
- Output
The operation returns the following document:
See also