$facet (aggregation)

On this page本页内容

Definition定义

$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阶段有以下形式:

{ $facet:
   {
      <outputField1>: [ <stage1>, <stage2>, ... ],
      <outputField2>: [ <stage1>, <stage2>, ... ],
      ...

   }
}

Specify the output field name for each specified pipeline.为每个指定的管道指定输出字段名。

Behavior行为

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>

Index Use索引使用

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

Example示例

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 are fetched from the database only once, at the beginning of the operation:在操作开始时,artwork中的输入文档仅从数据库中提取一次:

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 }
  ]
}