Dato-operatorerne skal bruges i et $project
operation, ikke en $group
, så du skal gøre det sådan i stedet (i skallen):
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'}
}});
Giver et resultat af:
{
"result": [
{
"_id": {
"year": 2012,
"month": 12,
"day": 19,
"hour": 10
},
"avg": 13.244705635
}
],
"ok": 1
}