在mongodb中对组查询的结果进行分组

时间:2016-08-01 14:03:35

标签: mongodb mongodb-query aggregation-framework

我有以下示例数据:

{
    "name": "Bob",
    "mi": "K",
    "martialStatus": "M",
    "age": 30,
    "city": "Paris",
    "job": "Engineer"
}
{
    "name": "Chad",
    "mi": "M",
    "martialStatus": "W",
    "age": 31,
    "city": "Paris",
    "job": "Doctor"
}
{
    "name": "Mel",
    "mi": "A",
    "martialStatus": "D",
    "age": 31,
    "city": "London",
    "job": "Doctor"
}
{
    "name": "Frank",
    "mi": "F",
    "martialStatus": "S",
    "age": 30,
    "city": "London",
    "job": "Engineer"
}

我正在尝试编写一个以下列格式返回结果的mongo查询:     “peopleCount”:4,     “jobsList”:{         “工作”:“医生”,         “ageList”:[             {                 “年龄”:31岁,                 “cityList”:[                     {                         “城市”:“伦敦”,                         “人”:[                             {                                 “名字”:“梅尔”,                                 “martialStatus”:“D”                             }                         ]                     },                     {                         “城市”:“巴黎”,                         “人”:[                             {                                 “名字”:“乍得”,                                 “martialStatus”:“W”                             }                         ]                     },                    {                         “城市”:“柏林”,                             ...                             ...                 ]             }         ]     }

尝试前两个级别(jobsList和ageList),我正在尝试下面的

db.colName.aggregate([
    { 
        $group: { 
            _id: { job: "$job" },
            jobsList: {
                $push: {
                    age: "$age",
                    city: "$city",
                    name: "$name",
                    martialStatus: "$martialStatus"
                }
            }
        }
    },
    {
        $group: {
            _id: { age: "$age" }, 
            ageList: {
                $push: {
                    city: "$city", 
                    name: "$name", 
                    martialStatus: "$martialStatus"
                }
            }
        }
    }
]); 

虽然上面的第一组/推送部分有效,但上述情况不起作用...有关如何获得输出格式/灌浆的任何提示?

1 个答案:

答案 0 :(得分:2)

db.colName.aggregate([
{
    $group: {
        _id: { job: "$job", age: "$age", city: "$city" },
        people: { $push: { name: "$name", martialStatus: "$martialStatus" } }
    }
},
{
    $group: {
        _id: { job: "$_id.job", age: "$_id.age" },
        peopleCount: { $sum: { $size: "$people" } },
        cityList: { $push: { city: "$_id.city", people: "$people" } },
    }
},
{
    $group: {
        _id: { job: "$_id.job" },
        peopleCount: { $sum: "$peopleCount" },
        agesList: { $push: { age: "$_id.age", cityList: "$cityList" } }
    }
},
{
    $group: {
        _id: null,
        peopleCount: { $sum: "$peopleCount" },
        jobsList: { $push: { job: "$_id.job", agesList: "$agesList" } }
    }
},
{
    $project: { _id: 0, peopleCount: 1, jobsList: 1 }
}
]);

在您提供的集合上给我结果

{
  "peopleCount" : 4,
  "jobsList" : 
  [ 
    { 
      "job" : "Engineer", 
      "agesList" : 
        [ 
          { 
            "age" : 30, 
            "cityList" : 
              [ 
                { 
                  "city" : "London", 
                  "people" : 
                    [ 
                      { "name" : "Frank", "martialStatus" : "S" } 
                    ] 
                }, 
                { 
                  "city" : "Paris", 
                  "people" : 
                    [ 
                      { "name" : "Bob", "martialStatus" : "M" } 
                    ] 
                } 
              ] 
          } 
        ]
    },
    { 
      "job" : "Doctor", 
      "agesList" : 
        [ 
          { 
            "age" : 31, 
            "cityList" : 
              [ 
                { 
                  "city" : "London", 
                  "people" : 
                    [ 
                      { "name" : "Mel", "martialStatus" : "D" } 
                    ] 
                }, 
                { 
                  "city" : "Paris", 
                  "people" : 
                    [ 
                      { "name" : "Chad", "martialStatus" : "W" } 
                    ] 
                } 
              ] 
          } 
        ] 
    } 
  ] 
}

这似乎是正确的。我想,我不确定它是最好的解决方案。我是聚合框架的新手。