Het zou niet de resultaten moeten opleveren die u ziet, maar u moet wel client_id
. opnemen in je $project
operator zodat deze beschikbaar is voor de $group
telefoniste.
'$project' => array(
'client_id' => 1,
'hour' => array(
'years' => array( '$year' => '$ts' ),
'months' => array( '$month' => '$ts' ),
'days' => array( '$dayOfMonth' => '$ts' ),
'hours' => array( '$hour' => '$ts' ),
)
),
Het shell-equivalent werkte nadat ik die wijziging had aangebracht:
db.test.aggregate(
{ $project: {
hour: {
years: {$year: '$ts'},
months: {$month: '$ts'},
days: {$dayOfMonth: '$ts'},
hours: {$hour: '$ts'}
},
client_id: '$client_id'
}},
{ $group: {
_id: { client_id: '$client_id', hour: '$hour' },
number: { $sum: 1}
}})
geretourneerd:
{
"result": [
{
"_id": {
"client_id": "12345667889",
"hour": {
"years": 2013,
"months": 1,
"days": 2,
"hours": 7
}
},
"number": 1
}
],
"ok": 1
}