随着aggregate
我检索按天分组的所有数据,这样:
db.collection('checkpoint').aggregate
[
{ '$match': {'id_journey': journey.id_journey} },
{ '$sort': { 'when': 1} },
{
'$group': {
'_id': { $dateToString: { format: '%Y-%m-%d', date: '$when' } },
'day': {
'$push': {
'id': '$id_checkpoint',
'when': '$when',
'type': '$type',
'url_media': '$url_media'
}
}
}
},
{ '$sort': { '_id': 1 } },
{
'$project': {
'_id': 1,
'day': 1
}
}
], function(err, result) {
res.json(result);
});
结果是:
[{
_id: "2016-04-22",
day: [{
id: "c571be034449bee845f2b43211",
when: "2016-04-22T20:51:00.190Z",
type: "picture",
url_media: "my_photo_1.jpg"
}]
}, {
_id: "2016-04-23",
day: [{
id: "c571be034449bee845f2b43222",
when: "2016-04-23T20:50:00.190Z",
type: "picture",
url_media: "my_photo_2.jpg"
}, {
id: "c571be034449bee845f2b43233",
when: "2016-04-23T20:51:00.190Z",
type: "picture",
url_media: "my_photo_3.jpg"
}]
}]
这是正确的。
但是,如果我需要在每天的时间内分组(使用 MongoDB的$hour
日期聚合运算符)怎么办?想要的结果是每个日组都细分为小时组:这可能吗?