So with "hours" actually being a field (property) within your document to begin with. So from the previous answer you just abstract the double grouping as follows:
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 }
);
So you are simply using $sum in both stages.
Additionally, It should be worthwhile for you to take a look at the SQL to Aggregation mapping chart provided in the official documentation. It has many examples of common SQL operations and how to implement them in a MongoDB way.
From your own data, but inserted by myself in this way:
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 }
])
And not the best example since all the months are actually different but this would get "distinct" values on the "holiday_type" if it needed to group that way. The result is achieved:
{
"_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
}