Seharusnya tidak membuat hasil yang Anda lihat, tetapi Anda harus menyertakan client_id
di $project
. Anda operator sehingga tersedia untuk $group
operator.
'$project' => array(
'client_id' => 1,
'hour' => array(
'years' => array( '$year' => '$ts' ),
'months' => array( '$month' => '$ts' ),
'days' => array( '$dayOfMonth' => '$ts' ),
'hours' => array( '$hour' => '$ts' ),
)
),
Setara shell berfungsi setelah saya melakukan perubahan itu:
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}
}})
dikembalikan:
{
"result": [
{
"_id": {
"client_id": "12345667889",
"hour": {
"years": 2013,
"months": 1,
"days": 2,
"hours": 7
}
},
"number": 1
}
],
"ok": 1
}