简体   繁体   English

如何在mongodb中按多个字段进行聚合

[英]How to aggregate by multiple fields in mongodb

I want to aggregate by 2 fields and wish to have the gouping done in a nested way. 我想按2个字段进行聚合,并希望以嵌套的方式完成分组。 How do I achieve it? 我该如何实现? Currently I do the grouping in following way 目前,我按照以下方式进行分组

var query = [
  { '$group': {
    '_id': {
      'employee': '$employee',
      'sector': '$sector'
    },
    'bookCount': { '$sum': 1 }
  }},
  { '$sort': { 'count': -1 } }
];

Order.aggregate(query, function(err, results){
  res.json(results)
});

I want the results to be in the form 我希望结果以表格形式显示

{abc:{sector1:4, sector3:5}, xyz: {sector1:10, sector2:23}}

where abc, xyz are employees and sector1, sector2 are sectors. 其中abc,xyz是员工,sector1,sector2是扇区。

How do I aggregate to get nested results? 如何汇总以获取嵌套结果?

My original document is 我的原始文件是

[
  {
    "sector": "sector1",
    "employee": "xyz"
  },
  {
    "sector": "sector1",
    "employee": "abc"
  },
  {
    "sector": "sector1",
    "employee": "abc"
  },
  {
    "sector": "sector2",
    "employee": "abc"
  }
]

I want result to be of the form 我希望结果的格式为

{abc:{sector1:2,sector2:2}, xyz: {sector1:1}}

You cannot use "data" as "key names" in the aggregation framework, and nor can you create nested objects with nested properties. 您不能在聚合框架中将“数据”用作“键名”,也不能创建具有嵌套属性的嵌套对象。 You shouldn't want to either, as this is an "anti-pattern". 您也不想要,因为这是“反模式”。 Data is data and should stay that way. 数据就是数据,应该保持这种状态。 Plus, there are better ways to do this: 另外,还有更好的方法可以做到这一点:

Order.aggregate([
    { "$group": {
        "_id": {
           "employee": "$employee",
           "sector": "$sector"
        },
        "count": { "$sum": 1 }
    }},
    { "$group": {
       "_id": "$_id.employee",
       "sectors": { 
           "$push": {
               "sector": "$_id.sector",
               "count": "$count"
           }
       }
    }}
],function(err,docs) {

});

Which returns a structure like this: 返回的结构如下:

[
    {
            "_id" : "xyz",
            "sectors" : [
                    {
                            "sector" : "sector1",
                            "count" : 1
                    }
            ]
    },
    {
            "_id" : "abc",
            "sectors" : [
                    {
                            "sector" : "sector2",
                            "count" : 1
                    },
                    {
                            "sector" : "sector1",
                            "count" : 2
                    }
            ]
    }
]

So you have a primary grouping key for "employee" values and the other results are "pushed" to an array. 因此,您有一个用于“员工”值的主分组键,而其他结果则被“推送”到一个数组中。

It's a much better structure with consistent results in the naming of keys. 这是一个更好的结构,在键命名方面具有一致的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM