On this page本页内容
$facet
Processes multiple aggregation pipelines within a single stage on the same set of input documents. 在同一组输入文档的单个阶段中处理多个聚合管道。Each sub-pipeline has its own field in the output document where its results are stored as an array of documents.每个子管道在输出文档中都有自己的字段,其结果存储为文档数组。
The $facet
stage allows you to create multi-faceted aggregations which characterize data across multiple dimensions, or facets, within a single aggregation stage. $facet
阶段允许您创建多方面聚合,在单个聚合阶段中跨多个维度或方面描述数据。Multi-faceted aggregations provide multiple filters and categorizations to guide data browsing and analysis. 多方面聚合提供多个筛选器和分类,以指导数据浏览和分析。Retailers commonly use faceting to narrow search results by creating filters on product price, manufacturer, size, etc.零售商通常通过创建产品价格、制造商、尺寸等筛选器来缩小搜索结果。
Input documents are passed to the 输入文档只传递到$facet
stage only once. $facet
阶段一次。$facet
enables various aggregations on the same set of input documents, without needing to retrieve the input documents multiple times.在同一组输入文档上启用各种聚合,而无需多次检索输入文档。
The $facet
stage has the following form:$facet
阶段具有以下形式:
{ $facet:
{
<outputField1>: [ <stage1>, <stage2>, ... ],
<outputField2>: [ <stage1>, <stage2>, ... ],
...
}
}
Specify the output field name for each specified pipeline.为每个指定管道指定输出字段名称。
The output of the $facet
stage is subject to the 16 megabyte BSON Document Size limit. $facet
阶段的输出受制于16兆字节的BSON文档大小限制。If the results of the 如果$facet
stage exceed the BSON Document Size limit, the aggregation produces an error.$facet
阶段的结果超过BSON文档大小限制,则聚合将产生错误。
Facet-related aggregation stages categorize and group incoming documents. 与方面相关的聚合阶段对传入文档进行分类和分组。Specify any of the following facet-related stages within different 在不同$facet
sub-pipeline's <stage>
to perform a multi-faceted aggregation:$facet
子管道的<stage>
中指定以下任何与facet相关的阶段,以执行多方面聚合:
Other aggregation stages can also be used with 其他聚合阶段也可以与$facet
with the following exceptions:$facet
一起使用,但有以下例外:
Each sub-pipeline within $facet
is passed the exact same set of input documents. $facet
中的每个子管道都传递了完全相同的输入文档集。These sub-pipelines are completely independent of one another and the document array output by each is stored in separate fields in the output document. 这些子管道彼此完全独立,每个子管道输出的文档数组存储在输出文档中的单独字段中。The output of one sub-pipeline can not be used as the input for a different sub-pipeline within the same 一个子管道的输出不能用作同一$facet
stage. $facet
阶段中不同子管道的输入。If further aggregations are required, add additional stages after 如果需要进一步聚合,请在$facet
and specify the field name, <outputField>
, of the desired sub-pipeline output.$facet
之后添加其他阶段,并指定所需子管道输出的字段名<outputField>
。
The $facet
stage, and its sub-pipelines, cannot make use of indexes, even if its sub-pipelines use $match
or if $facet
is the first stage in the pipeline. $facet
阶段及其子管道不能使用索引,即使其子管道使用$match
或$facet
是管道中的第一阶段。The $facet
stage will always perform a COLLSCAN
during execution.$facet
阶段将在执行期间始终执行COLLSCAN
。
Consider an online store whose inventory is stored in the following 考虑一家在线商店,其库存存储在以下artwork
collection:artwork
集合中:
{ "_id" : 1, "title" : "The Pillars of Society", "artist" : "Grosz", "year" : 1926, "price" : NumberDecimal("199.99"), "tags" : [ "painting", "satire", "Expressionism", "caricature" ] } { "_id" : 2, "title" : "Melancholy III", "artist" : "Munch", "year" : 1902, "price" : NumberDecimal("280.00"), "tags" : [ "woodcut", "Expressionism" ] } { "_id" : 3, "title" : "Dancer", "artist" : "Miro", "year" : 1925, "price" : NumberDecimal("76.04"), "tags" : [ "oil", "Surrealism", "painting" ] } { "_id" : 4, "title" : "The Great Wave off Kanagawa", "artist" : "Hokusai", "price" : NumberDecimal("167.30"), "tags" : [ "woodblock", "ukiyo-e" ] } { "_id" : 5, "title" : "The Persistence of Memory", "artist" : "Dali", "year" : 1931, "price" : NumberDecimal("483.00"), "tags" : [ "Surrealism", "painting", "oil" ] } { "_id" : 6, "title" : "Composition VII", "artist" : "Kandinsky", "year" : 1913, "price" : NumberDecimal("385.00"), "tags" : [ "oil", "painting", "abstract" ] } { "_id" : 7, "title" : "The Scream", "artist" : "Munch", "year" : 1893, "tags" : [ "Expressionism", "painting", "oil" ] } { "_id" : 8, "title" : "Blue Flower", "artist" : "O'Keefe", "year" : 1918, "price" : NumberDecimal("118.42"), "tags" : [ "abstract", "painting" ] }
The following operation uses MongoDB's faceting features to provide customers with the store's inventory categorized across multiple dimensions such as tags, price, and year created. 以下操作使用MongoDB的分面功能为客户提供商店的库存,这些库存在多个维度上分类,如标签、价格和创建年份。This 此$facet
stage has three sub-pipelines that use $sortByCount
, $bucket
, or $bucketAuto
to perform this multi-faceted aggregation. $facet
阶段有三个子管道,它们使用$sortByCount
、$bucket
或$bucketAuto
执行此多方面聚合。The input documents from 来自artwork的输入文档仅在操作开始时从数据库获取一次:artwork
are fetched from the database only once, at the beginning of the operation:
db.artwork.aggregate( [ { $facet: { "categorizedByTags": [ { $unwind: "$tags" }, { $sortByCount: "$tags" } ], "categorizedByPrice": [ // Filter out documents without a price e.g., _id: 7 { $match: { price: { $exists: 1 } } }, { $bucket: { groupBy: "$price", boundaries: [ 0, 150, 200, 300, 400 ], default: "Other", output: { "count": { $sum: 1 }, "titles": { $push: "$title" } } } } ], "categorizedByYears(Auto)": [ { $bucketAuto: { groupBy: "$year", buckets: 4 } } ] } } ])
The operation returns the following document:运算返回以下文档:
{ "categorizedByYears(Auto)" : [ // First bucket includes the document without a year, e.g., _id: 4 { "_id" : { "min" : null, "max" : 1902 }, "count" : 2 }, { "_id" : { "min" : 1902, "max" : 1918 }, "count" : 2 }, { "_id" : { "min" : 1918, "max" : 1926 }, "count" : 2 }, { "_id" : { "min" : 1926, "max" : 1931 }, "count" : 2 } ], "categorizedByPrice" : [ { "_id" : 0, "count" : 2, "titles" : [ "Dancer", "Blue Flower" ] }, { "_id" : 150, "count" : 2, "titles" : [ "The Pillars of Society", "The Great Wave off Kanagawa" ] }, { "_id" : 200, "count" : 1, "titles" : [ "Melancholy III" ] }, { "_id" : 300, "count" : 1, "titles" : [ "Composition VII" ] }, { // Includes document price outside of bucket boundaries, e.g., _id: 5 "_id" : "Other", "count" : 1, "titles" : [ "The Persistence of Memory" ] } ], "categorizedByTags" : [ { "_id" : "painting", "count" : 6 }, { "_id" : "oil", "count" : 4 }, { "_id" : "Expressionism", "count" : 3 }, { "_id" : "Surrealism", "count" : 2 }, { "_id" : "abstract", "count" : 2 }, { "_id" : "woodblock", "count" : 1 }, { "_id" : "woodcut", "count" : 1 }, { "_id" : "ukiyo-e", "count" : 1 }, { "_id" : "satire", "count" : 1 }, { "_id" : "caricature", "count" : 1 } ] }