mongodb mongodb聚合排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12451322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:49:58  来源:igfitidea点击:

mongodb aggregation sort

mongodb

提问by Paul Gorton

I have a database of students and their contact details. I'm trying to find out the postcode that houses the most students. The documents for the students look something like this...

我有一个学生及其联系方式的数据库。我试图找出学生最多的邮政编码。学生的文件看起来像这样......

{studentcode: 'smi0001', firstname: 'bob', surname: 'smith', postcode: 2001}

{学生代码:'smi0001',名字:'bob',姓氏:'smith',邮政编码:2001}

I thought I could use the aggregation framework to find out the postcode with the most students by doing something like...

我想我可以使用聚合框架通过做类似的事情来找出最多学生的邮政编码......

db.students.aggregate({$project: { postcode: 1 }, $group: {_id: '$postcode', students: {$sum: 1}}})

this works as expected (returns postcodes as _idand the number of students in each postcode as 'students', but if I add $sortto the pipeline it seems to try sorting by the whole student collection instead of the results of the $groupoperation.

这按预期工作(返回邮政编码_id以及每个邮政编码中的学生人数作为“学生”,但如果我添加$sort到管道中,它似乎尝试按整个学生集合而不是$group操作结果进行排序。

what I'm trying look like...

我正在尝试的样子...

db.students.aggregate({$project: { postcode: 1 }, $group: {_id: '$postcode', students: {$sum: 1}}, $sort: {_id: -1}})

but it returns the whole collection and disregards the $projectand $group... Am I missing something? I thought I'd just be able to sort by descending number of students and return the first item. Thanks in advance for any help.

但它返回整个集合并无视$project$group......我错过了什么吗?我以为我只能按学生人数降序排序并返回第一项。在此先感谢您的帮助。

回答by Eve Freeman

You almost had it...

你几乎拥有它......

db.test.aggregate(
  {$group: {_id: '$postcode', students: {$sum: 1}}}, 
  {$sort: {_id: -1}}
);

gives (I added some test data matching your sample):

给出(我添加了一些与您的样本匹配的测试数据):

{
  "result" : [
    {
        "_id" : 2003,
        "students" : 3
    },
    {
        "_id" : 2002,
        "students" : 1
    },
    {
        "_id" : 2001,
        "students" : 2
    }
  ],
  "ok" : 1
}

You had an outer {}around everything, which was causing some confusion. The group and sort weren't working as separate operations in the pipeline.

{}在所有东西周围都有一个外部,这引起了一些混乱。group 和 sort 没有在管道中作为单独的操作工作。

You didn't really need the project for this case.

对于这种情况,您实际上并不需要该项目。

UpdateYou probably want to sort by "students", like so, to get the biggest zipcodes (by population) first:

更新您可能想按“学生”排序,像这样,首先获得最大的邮政编码(按人口):

db.test.aggregate(
  {$group: {_id: '$postcode', students: {$sum: 1}}}, 
  {$sort: {students: -1}}
);

回答by Thomas

I think your syntax is slightly wrong. Each aggregation operation in the pipeline should be its own document.

我认为你的语法有点错误。管道中的每个聚合操作都应该是它自己的文档。

db.students.aggregate( {$project: ...}, {$group: ...}, {$sort: ...} )

In your case, it should be:

在你的情况下,它应该是:

db.students.aggregate(
    {$project: { postcode: 1 }}, 
    {$group: {_id: '$postcode', students: {$sum: 1}}}, 
    {$sort: {students: -1}}
)

I've tested it on a sample collection based on your schema and it works for me, sorting the grouped post codes by number of students, descending.

我已经根据您的模式在示例集合上对其进行了测试,它对我有用,按学生人数对分组的邮政编码进行排序,降序排列。