Grouping the results of a group query in mongodb -


i have following sample data:

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

i trying write mongo query return results in following format: "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", ... ... ] } ] }

to try on first 2 level (jobslist , agelist), trying below

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

the above not work although first group/push part works... hints on how output format/groupping?

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

on provided collection gives me result

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

that seems correct. thought, not sure it's best solution. new aggregation-framework.


Comments