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

我有以下示例数据:

{
    "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"
                }
            }
        }
    }
]); 

虽然第一组/推动部件工作,但上述方法不起作用…有关如何获得输出格式/灌浆的任何提示?

最佳答案

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

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

点赞