0

この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 Date Aggregation Operatorsを使用して) 1 時間ごとにグループ化する必要がある場合はどうすればよいでしょうか? 必要な結果は、各日のグループが時間のグループに細分化されることです:それは可能ですか?

4

1 に答える 1