On this page本页内容
$facet
¶New in version 3.4.版本3.4中的新功能。
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.$facet
支持对同一组输入文档进行各种聚合,而无需多次检索输入文档。
The $facet
stage has the following form:$facet
阶段有以下形式:
Specify the output field name for each specified pipeline.为每个指定的管道指定输出字段名。
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
集合中:
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
are fetched from the database only once, at the beginning of the operation:artwork
中的输入文档仅从数据库中提取一次:
The operation returns the following document:该操作将返回以下文档: