Operator tanggal harus digunakan dalam $project
operasi, bukan $group
, jadi Anda harus melakukannya seperti ini (di shell):
db.tickers.aggregate(
{ $project: {
_id: 0,
year: {$year: '$date'},
month: {$month: '$date'},
day: {$dayOfMonth: '$date'},
hour: {$hour: '$date'},
avg: '$ticker.avg'
}},
{ $group: {
_id: { year: '$year', month: '$month', day: '$day', hour: '$hour' },
avg: { $avg: '$avg'}
}});
Memberikan hasil dari:
{
"result": [
{
"_id": {
"year": 2012,
"month": 12,
"day": 19,
"hour": 10
},
"avg": 13.244705635
}
],
"ok": 1
}