12

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}}

3 Answers 3

11

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}}
1
  • I think you could also do {$sort: {"date": 1} }
    – knowbody
    May 29, 2014 at 16:41
6

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);
-1

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

1
  • You should update your answer with your correction, not put it as a comment.
    – drneel
    Aug 14, 2015 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.