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

rkh*_*rkh 5 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"
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试编写一个 mongo 查询,该查询将以以下格式返回结果: "peopleCount": 4, "jobsList": { "job": "Doctor", "ageList": [ { "age": 31, "cityList ": [ { "city": "London", "people": [ { "name": "Mel", "martialStatus": "D" } ] }, { "city": "Paris", "people":[ { "name": "Chad", "martialStatus": "W" } ] }, { "city": "Berlin", ... ... ] } ] }

要尝试前两个级别(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"
                }
            }
        }
    }
]); 
Run Code Online (Sandbox Code Playgroud)

尽管第一组/推送部分有效,但上述内容不起作用......有关如何获得该输出格式/分组的任何提示?

tar*_*pka 5

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 }
}
]);
Run Code Online (Sandbox Code Playgroud)

您提供的集合给了我结果

{
  "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" } 
                    ] 
                } 
              ] 
          } 
        ] 
    } 
  ] 
}
Run Code Online (Sandbox Code Playgroud)

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