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参阅