MongoDB聚合:$facet

对输入的文档执行多个聚合管道,在输出结果中,每个子管道一个字段,字段值是一个文档数组。

$facet可以在一个阶段创建多面聚合,横跨多个维度或方面来描述数据特征。多面聚合可提供多个过滤器和分类指导数据浏览和分析。

$facet 阶段在单个聚合阶段内创建多面聚合,通过多个维度或面来描述数据特征。多面聚合提供了多个过滤器和分类,为数据浏览和分析导航,零售商通常通过创建产品价格、制造商、尺寸等方面的过滤器以及使用分面来缩小搜索范围。

输入文件只传递给$facet阶段一次。$facet可对同一组输入文档进行各种聚合,而无需多次检索输入文档。

语法

$facet阶段使用方法如下:

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

    }
}

使用

  • 注意,$facet的每个阶段执行时,返回文档大小不能超过100M,而且由于$facet不能缓存到磁盘,所以即使指定了allowDiskUse标志也没用。

  • 指定下面任何一个方面相关的阶段在不同的$facet子管道的<stage去执行一个已多方面的聚合.

    • $bucket
    • $bucketAuto
    • $sortByCount
  • 下面阶段中不能使用$facet

    • $collStats
    • $facet
    • $geoNear
    • $indexStats
    • $out
    • $merge
    • $planCacheStats
  • $facet的所有子管道都传递完全相同的输入文档,这些子管道彼此独立,每个子管道输出的文档数组都存储在各自的字段中。另外,一个子管道的输出不能用作同一个$facet阶段内其它子管道的输入。如果需要进一步聚合,可以在$facet后面添加其他阶段。

  • $facet 中的每个子管道都会传递完全相同的输入文档集。这些子流水线彼此完全独立,每个子流水线输出的文档数组都存储在输出文档的不同字段中。一个子管道的输出不能用作同一 $facet 阶段内不同子管道的输入。如果需要进一步聚合,可以在\$facet的后面添加其他阶段,并指定所需次级管道输出的字段名称 <outputField>

  • 管道的顺序决定了$facet使用索引:

    • 如果$facet是管道的第一个阶段,将会执行COLLSCAN,不会使用索引。
    • 如果$facet在其他阶段之后,并且先前的阶段已经使用了索引,则$facet在执行的时候不会触发COLLSCAN。比如,如果$match$sort阶段在$facet之前,则$facet使用索引并且不会触发COLLSCAN

举例

以下在线艺术品商店库存中存在以下的艺术藏品:

{
    "_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" ] }

下面的操作使用MongoDB的分面功能,为客户提供按标签、价格和创建年份等多个维度分类的商店库存。$facet阶段有三个子管道,分别使用$sortByCount$bucket$bucketAuto来执行多面聚合。输入文档只在操作开始时从数据库中获取一次:

db.artwork.aggregate( [
  {
   
    $facet: {
   
      "categorizedByTags": [
        {
    $unwind: "$tags" },
        {
    $sortByCount: "$tags" }
      ],
      "categorizedByPrice": [
        //过滤掉缺失价格的文档 如:_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
          }
        }
      ]
    }
  }
])

操作返回结果:

{
   
  "categorizedByYears(Auto)" : [
    // 第一个桶包括不带年份的文件,如:_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"
      ]
    },
    {
   
      //包含桶边界外的文档的价格 如:_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 }
  ]
}

相关推荐

  1. MongoDB聚合:$facet

    2024-01-05 11:12:02       55 阅读
  2. MongoDB聚合管道:$match

    2024-01-05 11:12:02       50 阅读
  3. MongoDB聚合:$out

    2024-01-05 11:12:02       59 阅读
  4. MongoDB聚合:$replaceRoot

    2024-01-05 11:12:02       54 阅读
  5. MongoDB聚合:$addField

    2024-01-05 11:12:02       63 阅读
  6. MongoDB聚合:$bucket

    2024-01-05 11:12:02       46 阅读
  7. MongoDB聚合:$bucketAuto

    2024-01-05 11:12:02       49 阅读
  8. MongoDB聚合:$documents

    2024-01-05 11:12:02       56 阅读
  9. MongoDB聚合:$changeStreamSplitLargeEvent

    2024-01-05 11:12:02       55 阅读
  10. MongoDB聚合:$indexStats

    2024-01-05 11:12:02       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-05 11:12:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-05 11:12:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-05 11:12:02       82 阅读
  4. Python语言-面向对象

    2024-01-05 11:12:02       91 阅读

热门阅读

  1. stream流做的那些事

    2024-01-05 11:12:02       54 阅读
  2. 【taro react】 ---- QRCode 二维码生成

    2024-01-05 11:12:02       64 阅读
  3. 30分钟也能“玩转” K8s?新手必看!

    2024-01-05 11:12:02       41 阅读
  4. 1.4 力扣回溯题

    2024-01-05 11:12:02       66 阅读
  5. Hadoop的启动停止命令详解

    2024-01-05 11:12:02       59 阅读
  6. golang中net/http/server.go源码剖析

    2024-01-05 11:12:02       72 阅读
  7. Linux端口转发的几种常用方法

    2024-01-05 11:12:02       60 阅读