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 }
  ]
}
  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

原子星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值