Mongodb计数的不同,sum函数与group by multiple列

时间:2014-05-14 10:53:17

标签: mongodb aggregation-framework

我有一个交易表,由员工带来的假期填充。我需要帮助在mongodb中跟踪sql场景。

选择员工,月,年,计数(distinct(holiday_type),sum(hours)from 按员工,月,年划分的交易组

几个星期前我已经开始了mongodb。我得到的部分答案是堆栈溢出帖子Mongodb count distinct with multiple group fields,现在我想添加sum函数。

任何指导都非常有用,以下是表格形式的数据样本:

Employee    date      holiday_type  hours
1           1/1/2014  1             8 
1           1/5/2014  2             7 
1           2/15/2014 1             8 
1           3/15/2014 3             16 
11          1/1/2014  1             8 
11          1/5/2014  1             6 
11          2/15/2014 3             8 
11          3/15/2014 3             8

1 个答案:

答案 0 :(得分:1)

所以"小时"实际上是文档中的一个字段(属性)。因此,从前面的答案中,您只需将双重分组抽象如下:

db.transactions.aggregate([
    { "$group": { 
        "_id": { 
            "employee" : "$employee",
            "Month": { "$month" : "$date" }, 
            "Year": { "$year" : "$date" },
            "holiday_type" : "$holiday_type"
        },
        "hours": { "$sum": "$hours" }
     }},
     { "$group": {
         "_id": {
            "employee" : "$_id.employee",
            "Month": "$_id.Month",
            "Year": "$_id.Year"
         },
         "count": { "$sum": 1 },
         "hours": { "$sum": "$hours" }
     }}
 ], { "allowDiskUse": true }
 );

所以你只是在两个阶段都使用$sum

此外,您应该看看官方文档中提供的SQL to Aggregation mapping chart是值得的。它有许多常见的SQL操作示例以及如何以MongoDB方式实现它们。


来自您自己的数据,但我自己也是这样插入的:

db.transactions.insert([
    { "employee": 1,  "date": new Date("2014-01-01"), "holiday_type":  1, "hours": 8   },
    { "employee": 1,  "date": new Date("2014-01-05"), "holiday_type":  2, "hours": 7   },
    { "employee": 1,  "date": new Date("2014-02-15"), "holiday_type":  1, "hours": 8   },
    { "employee": 1,  "date": new Date("2014-03-15"), "holiday_type":  3, "hours": 16  },
    { "employee": 11, "date": new Date("2014-01-01"), "holiday_type":  1, "hours": 8   },
    { "employee": 11, "date": new Date("2014-01-05"), "holiday_type":  1, "hours": 6   },
    { "employee": 11, "date": new Date("2014-02-15"), "holiday_type":  1, "hours": 8   },
    { "employee": 11, "date": new Date("2014-03-15"), "holiday_type":  3, "hours": 8   }
])

并不是最好的例子,因为所有月份实际上都是不同的,但这会得到“不同的”#34; " holiday_type"上的值如果它需要这样分组。结果是:

{
    "_id" : {
            "employee" : 1,
            "Month" : 2,
            "Year" : 2014
    },
    "count" : 1,
    "hours" : 8
}
{
    "_id" : {
            "employee" : 11,
            "Month" : 2,
            "Year" : 2014
    },
    "count" : 1,
    "hours" : 8
}
{
    "_id" : {
            "employee" : 1,
            "Month" : 1,
            "Year" : 2014
    },
    "count" : 2,
    "hours" : 15
}
{
    "_id" : {
            "employee" : 11,
            "Month" : 1,
            "Year" : 2014
    },
    "count" : 1,
    "hours" : 14
}
{
    "_id" : {
            "employee" : 1,
            "Month" : 3,
            "Year" : 2014
    },
    "count" : 1,
    "hours" : 16
}
{
    "_id" : {
            "employee" : 11,
            "Month" : 3,
            "Year" : 2014
    },
    "count" : 1,
    "hours" : 8
}