2016-08-01 79 views
3

结果我有下面的示例数据:分组一组查询的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" 
} 

我想写一个蒙戈查询将在下面的格式返回结果: “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
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" } 
        ] 
       } 
       ] 
      } 
     ] 
    } 
    ] 
} 

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

+0

这按预期工作。谢谢你和开始聚合框架的好方法:)。 – rkh

+0

是否可以在joblist之前显示如何在顶部添加另一个参数:“peopleCount”:4 – rkh

+0

您的情况可能是一个人住在多个城市或有多个工作,因此在您的收藏中会有几个文件对应一个人? – tarashypka