Sort by date in mongoose aggregation framework

拜拜、爱过 提交于 2020-08-01 09:58:27

问题


Im working on a nodejs+mongodb project using mongoose. Now I have come across a question I don't know the answer to. I am using aggregation framework to get grouped results. The grouping is done on a date excluding time data field like: "2013 02 06". Code looks like this:

MyModel.aggregate([
            {$match: {$and: [{created_date: {$gte: start_date}}, {created_date: {$lte: end_date}}]}},
            {$group: {
                _id: {
                    year: {$year: "$created_at"},
                    month: {$month: "$created_at"},
                    day: {$dayOfMonth: "$created_at"}
                },
                count: {$sum: 1}
            }},
            {$project: {
                date: {
                        year: "$_id.year",
                        month:"$_id.month",
                        day:"$_id.day"
                },
                count: 1,
                _id: 0
            }}
        ], callback);

The grouped results are perfect, except that they are not sorted. Here is an example of output:

[
    {
        count: 1,
        date: {
            year: 2013,
            month: 2,
            day: 7
        }
    },
    {
        count: 1906,
        date: {
            year: 2013,
            month: 2,
            day: 4
        }
    },
    {
        count: 1580,
        date: {
            year: 2013,
            month: 2,
            day: 5
        }
    },
    {
        count: 640,
        date: {
            year: 2013,
            month: 2,
            day: 6
        }
    }
]

I know the sorting is done by adding this: {$sort: val}. But now I'm not sure what should be the val so the results would be sorted by date as my grouping key es an object of 3 values constructing the date. Does anyone know how this could be accomplished?

EDIT Have tryed this and it worked :)

{$sort: {"date.year":1, "date.month":1, "date.day":1}}


回答1:


It appears that this question has a very simple answer :) Just need to sort by multiple nesteed columns like this:

{$sort: {"date.year":1, "date.month":1, "date.day":1}}



回答2:


I got stuck with the same problem, thanks for your answer. But I found out that you can get the same result with less code

MyModel.aggregate([
            {$match: {$and: [{created_date: {$gte: start_date}}, {created_date: {$lte: end_date}}]}},
            {$group: {
                _id: {
                    year: {$year: "$created_at"},
                    month: {$month: "$created_at"},
                    day: {$dayOfMonth: "$created_at"}
                },
                count: {$sum: 1}
            }},
            {$project: {
                date: "$_id", // so this is the shorter way
                count: 1,
                _id: 0
            }},
            {$sort: {"date": 1} } // and this will sort based on your date
        ], callback);



回答3:


This would work if you are only sorting by date if you had other columsn to sort on. YOu would need to expand _id



来源:https://stackoverflow.com/questions/14747108/sort-by-date-in-mongoose-aggregation-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!