aggregation之$facet

$facet语法
{ $facet:
   {
      <outputField1>: [ <stage1>, <stage2>, ... ],
      <outputField2>: [ <stage1>, <stage2>, ... ],
      ...
   }
}
个人理解

通过一次查询,获取多个维度的聚合结果,上面的语法等价于:

## 多次聚合查询
aggregate([ <stage1>, <stage2>, ... ]);
aggregate([ <stage1>, <stage2>, ... ]);
···
准备测试数据:
db.artwork.insertMany([{ "_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" ] }])

进行聚合查询
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
          }
        }
      ]
    }
  }
])

等价于:

## 查询语句一:
db.artwork.aggregate([  
    { $unwind: "$tags" },
    { $sortByCount: "$tags" }
])
## 查询语句二:
db.artwork.aggregate([  
  { $match: { price: { $exists: 1 } } },
        {
          $bucket: {
            groupBy: "$price",
            boundaries: [  0, 150, 200, 300, 400 ],
            default: "Other",
            output: {
              "count": { $sum: 1 },
              "titles": { $push: "$title" }
            }
          }
        }
])
## 查询语句仨:
db.artwork.aggregate([  
{
          $bucketAuto: {
            groupBy: "$year",
            buckets: 4
          }
        }
])
得到相应结果:
{
  categorizedByTags: [
    {_id:'painting',count:6},{_id:'oil',count:4},{_id:'Expressionism',count:3},
    {_id:'Surrealism',count:2},{_id:'abstract',count:2},{_id:'satire',count:1},
    {_id:'caricature',count:1},{_id:'ukiyo-e',count:1},{_id:'woodblock',count:1},
    {_id:'woodcut',count:1}
  ],
  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:'Other',count:1,titles:['The Persistence of Memory']}
  ],
  'categorizedByYears(Auto)':[
    {_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}
  ]
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值