mongodb 单个操作中的多个聚合

标签 mongodb aggregation-framework

我有一个包含以下文档的项目集合。

{ "item" : "i1", "category" : "c1", "brand" : "b1" }  
{ "item" : "i2", "category" : "c2", "brand" : "b1" }  
{ "item" : "i3", "category" : "c1", "brand" : "b2" }  
{ "item" : "i4", "category" : "c2", "brand" : "b1" }  
{ "item" : "i5", "category" : "c1", "brand" : "b2" }  

我想分离聚合结果 --> 按类别计数,按品牌计数。请注意,它不是按(类别、品牌)计算的

我可以使用以下代码使用 map-reduce 来做到这一点。

map = function(){
    emit({type:"category",category:this.category},1);
    emit({type:"brand",brand:this.brand},1);
}
reduce = function(key, values){
    return Array.sum(values)
}
db.item.mapReduce(map,reduce,{out:{inline:1}})

结果是

{
        "results" : [
                {
                        "_id" : {
                                "type" : "brand",
                                "brand" : "b1"
                        },
                        "value" : 3
                },
                {
                        "_id" : {
                                "type" : "brand",
                                "brand" : "b2"
                        },
                        "value" : 2
                },
                {
                        "_id" : {
                                "type" : "category",
                                "category" : "c1"
                        },
                        "value" : 3
                },
                {
                        "_id" : {
                                "type" : "category",
                                "category" : "c2"
                        },
                        "value" : 2
                }
        ],
        "timeMillis" : 21,
        "counts" : {
                "input" : 5,
                "emit" : 10,
                "reduce" : 4,
                "output" : 4
        },
        "ok" : 1,
}

我可以通过触发以下两个不同的聚合命令来获得相同的结果。

db.item.aggregate({$group:{_id:"$category",count:{$sum:1}}})
db.item.aggregate({$group:{_id:"$brand",count:{$sum:1}}})

无论如何我可以通过单个聚合命令使用聚合框架来做同样的事情。

我在这里简化了我的案例,但实际上我需要从子文档数组中的字段进行分组。假设以上是我放松后的结构。

这是一个实时查询(有人等待响应),虽然在较小的数据集上,所以执行时间很重要。

我正在使用 MongoDB 2.4。

最佳答案

Mongo 3.4 开始,$facet聚合阶段通过在同一输入文档集的单个阶段内处理多个聚合管道,极大地简化了此类用例:

// { "item" : "i1", "category" : "c1", "brand" : "b1" }
// { "item" : "i2", "category" : "c2", "brand" : "b1" }
// { "item" : "i3", "category" : "c1", "brand" : "b2" }
// { "item" : "i4", "category" : "c2", "brand" : "b1" }
// { "item" : "i5", "category" : "c1", "brand" : "b2" }
db.collection.aggregate(
  { $facet: {
      categories: [{ $group: { _id: "$category", count: { "$sum": 1 } } }],
      brands:     [{ $group: { _id: "$brand",    count: { "$sum": 1 } } }]
  }}
)
// {
//   "categories" : [
//     { "_id" : "c1", "count" : 3 },
//     { "_id" : "c2", "count" : 2 }
//   ],
//   "brands" : [
//     { "_id" : "b1", "count" : 3 },
//     { "_id" : "b2", "count" : 2 }
//   ]
// }

关于mongodb 单个操作中的多个聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23384244/

相关文章:

arrays - 计算数组和无限子数组中的所有对象

node.js - 为什么当我从 postman 中点击以下功能时会卡住?

mongodb - "group by"对 meteor 收集的查询

mongodb - 如何检查mongo中相同文档中的两个值?

c++ - C++ 中的 MongoDB $group 命令

mongodb 聚合查询在使用 $sum 时没有返回正确的总和

javascript - 通过socket.io将Mongo连接传递给JQuery代码?

node.js - 在 nodejs mongodb 中创建 Array 对象

mongodb - 在MongoDB中将字段乘以值

php - 如何在 phalcon 中将 "group by"、 "having"查询从 mysql 转换为 mongodb