根据每个城市中有多少患者查找最高的医生

时间:2016-11-28 10:01:03

标签: mongodb mongodb-query aggregation-framework

我想创建一个聚合查询,显示每个城市中患者最多的医生。这也适用于每个城市的平均年龄。

例如:

{
     City: "Vancouver"
     Physician: "Physician Test1"
     Average_Age: 56
},
{
     City: "Burnaby"
     Physician: "Physician Test2"
     Average_Age: 40
}

我的部分数据示例

{
        "gender" : "Female",
        "full_name" : "Test Patient",
        "age" : 20,
        "city" : "Burnaby",
        "severity_code" : 2,
        "PHN" : "11-1111111",
        "primary_physician" : "Physician Test1",
        "location" : "4A",
        "attendance_method" : "Self-Driven"
},
{
        "gender" : "Male",
        "full_name" : "Test2 Patient2",
        "age" : 68,
        "city" : "Vancouver",
        "severity_code" : 1,
        "PHN" : "00-0000000",
        "primary_physician" : "Physician Test2",
        "location" : "6D",
        "attendance_method" : "Walk-In"
 }

如何为上述创建聚合查询?

1 个答案:

答案 0 :(得分:0)

您可以运行以下聚合管道:

db.patients.aggregate([
    {
        "$group": {
            "_id": {
                "City": "$city",
                "Physician": "$primary_physician"
            },
            "count": { "$sum": 1 },
            "Average_Age": { "$avg": "$age" }               
        }
    },
    { "$sort": { "count": -1 } },
    {
        "$group": {
            "_id": "$_id.City"
            "Physician": { "$first": "$_id.Physician" },
            "Number_of_Patients": { "$first": "$count" },
            "Average_Age": { "$first": "$Average_Age" }         
        }
    }   
])

在上述聚合中,带有 $group 运算符的第一个管道步骤按cityphysician字段对文档进行分组,并计算文档数(通过{ "$sum": 1 }表达,以及将患者的平均年龄与{ "$avg": "$age" }表达进行汇总,从而(即病人数)。

第二个管道阶段{ "$sort": { "count": -1 } }按计数对先前分组的文档进行排序。这对于进一步分组是必要的,因为您希望获得每个physician中具有最多患者的city,即在每个组中,获取具有最大计数的顶部文档。前面的管道就是这样做的。

city键对已排序的文档进行分组,然后使用 $first 运算符返回所需的字段。