Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting mongoose聚合框架中的按日期排序_Sorting_Mongoose_Aggregation Framework - Fatal编程技术网

Sorting mongoose聚合框架中的按日期排序

Sorting mongoose聚合框架中的按日期排序,sorting,mongoose,aggregation-framework,Sorting,Mongoose,Aggregation Framework,我正在使用mongoose进行nodejs+mongodb项目。现在我遇到了一个我不知道答案的问题。 我使用聚合框架来获得分组结果。分组在不包括时间的日期数据字段上完成,如:“2013 02 06”。代码如下所示: MyModel.aggregate([ {$match: {$and: [{created_date: {$gte: start_date}}, {created_date: {$lte: end_date}}]}}, {$group:

我正在使用mongoose进行nodejs+mongodb项目。现在我遇到了一个我不知道答案的问题。 我使用聚合框架来获得分组结果。分组在不包括时间的日期数据字段上完成,如:“2013 02 06”。代码如下所示:

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);
{$sort: {"date.year":1, "date.month":1, "date.day":1}}
分组结果是完美的,只是没有排序。以下是一个输出示例:

[
    {
        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
        }
    }
]
我知道排序是通过添加以下内容完成的:
{$sort:val}
。但是现在我不确定什么应该是
val
,因此结果将按日期排序,因为我的分组键将生成一个包含3个值的对象来构造日期。有人知道如何做到这一点吗

编辑 我尝试过这一点,它成功了:)


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

这个问题似乎有一个非常简单的答案:)只需按多个嵌套列进行排序,如下所示:

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);
{$sort: {"date.year":1, "date.month":1, "date.day":1}}

我也遇到了同样的问题,谢谢你的回答。 但是我发现你可以用更少的代码得到同样的结果

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

如果您只按日期排序,并且有其他列可供排序,那么这将起作用。你需要扩展_id

我想你也可以做{$sort:{“date”:1}}你应该用你的更正更新你的答案,而不是把它作为评论。