On this page本页内容
$bucket
¶New in version 3.4.版本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 每个输出文档都包含一个_id
field whose value specifies the inclusive lower bound of the bucket. _id
字段,其值指定bucket的包含下限。The output option specifies the fields included in each output document.output
选项指定每个输出文档中包含的字段。
$bucket
only produces output documents for buckets that contain at least one input document.仅为至少包含一个输入文档的存储桶生成输出文档。
The $bucket
document contains the following fields:$bucket
文档包含以下字段:
groupBy | expression |
|
boundaries | array |
Example
|
default |
| |
output | document |
|
$bucket
requires at least one of the following conditions to be met or the operation throws an error:要求至少满足以下条件之一,否则操作引发错误:
groupBy
表达式解析为boundaries
指定的一个存储桶范围内的值,或groupBy
values are outside of the boundaries
or of a different BSON type than the values in boundaries
.groupBy
值在boundaries
之外或与boundaries
中的值不同的BSON类型的存储桶文档指定默认值。If the 如果groupBy
expression resolves to an array or a document, $bucket
arranges the input documents into buckets using the comparison logic from $sort
.groupBy
表达式解析为数组或文档,$bucket
使用$sort
中的比较逻辑将输入文档排列到bucket中。
From the 从mongo
shell, create a sample collection named artists
with the following documents:mongo
shell中,创建一个名为artists
的样本集合,并附带以下文档:
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:year_born
字段将文档分组到存储桶中,并根据存储桶中的文档计数进行筛选:
The $bucket
stage groups the documents into buckets by the year_born
field. $bucket
阶段按year_born
字段将文档分组到存储桶中。The buckets have the following boundaries:存储桶具有以下boundaries
:
1840
and exclusive upper bound 1850
.1850
and exclusive upper bound 1860
.1860
and exclusive upper bound 1870
.1870
and exclusive upper bound 1880
.year_born
field or its year_born
field was outside the ranges above, it would be placed in the default bucket with the _id
value "Other"
.The stage includes the output document to determine the fields to return:该阶段包括输出文档,用于确定要返回的字段:
_id |
|
count |
|
artists |
|
This stage passes the following documents to the next stage:本阶段将以下文件传递到下一阶段:
The $match
stage filters the output from the previous stage to only return buckets which contain more than 3 documents.$match
阶段筛选前一阶段的输出,只返回包含3个以上文档的存储桶。
The operation returns the following document:该操作将返回以下文档:
You can use the 可以使用$facet
stage to perform multiple $bucket
aggregations in a single stage.$facet
阶段在单个阶段中执行多个$bucket
聚合。
From the 在mongo
shell, create a sample collection named artwork
with the following documents:mongo
shell中,创建一个名为artwork
的样本集合,其中包含以下文档:
The following operation uses two 以下操作使用$bucket
stages within a $facet
stage to create two groupings, one by price
and the other by year
:$facet
阶段中的两个$bucket
阶段创建两个分组,一个按price
,另一个按year
:
The first facet groups the input documents by 第一个方面按price
. price
对输入文档进行分组。The buckets have the following boundaries:铲斗具有以下边界:
0
and exclusive upper bound 200
.200
and exclusive upper bound 400
.default
bucket containing documents without prices or prices outside the ranges above.The $bucket
stage includes the output document to determine the fields to return:
_id |
|
count |
|
artwork |
|
averagePrice |
$avg operator to display the average price of all artwork in the bucket.$avg 运算符显示桶中所有艺术品的平均价格。 |
The second facet groups the input documents by 第二个方面按year
. year
对输入文档进行分组。The buckets have the following boundaries:铲斗具有以下边界:
1890
and exclusive upper bound 1910
.1890
年的下限和排除1910
年的上限。1910
and exclusive upper bound 1920
.1910
年的下限和排除1920
年的上限。1910
and exclusive upper bound 1940
.1910
年的下限和排除1940
年的上限。default
bucket containing documents without years or years outside the ranges above.default
存储桶中包含的文档没有年份或年份超出上述范围。The $bucket
stage includes the output document to determine the fields to return:$bucket
阶段包括用于确定要返回的字段的输出文档:
count |
|
artwork |
The operation returns the following document:该操作将返回以下文档:
See also参阅