Is it possible to group and sum multiple columns with MongoDB's aggregation framework? -


given mongodb collection:

[   { character: 'broquaint', race: 'halfling', class: 'hunter' },   { character: 'broquaint', race: 'halfling', class: 'hunter' },   { character: 'broquaint', race: 'halfling', class: 'rogue' },   { character: 'broquaint', race: 'naga',     class: 'fighter' },   { character: 'broquaint', race: 'naga',     class: 'hunter' } ] 

i count of each race , class i.e

{   race:  { 'halfling': 3, 'naga': 2 },   class: { 'hunter': 3, 'rogue': 1, 'fighter': 1 } } 

and i've been trying using aggregation framework (to replace existing map/reduce) have been able far getting counts combinations i.e

{ '_id': { race: 'halfling', class: 'hunter' },  count: 2 } { '_id': { race: 'halfling', class: 'rogue' }    count: 1 } { '_id': { race: 'naga',     class: 'fighter' }, count: 1 } { '_id': { race: 'naga',     class: 'hunter' },  count: 1 } 

which simple enough reduce programmatically desired result hoping able leave mongodb.

for reference here's code have far:

db.games.aggregate(   { '$match': { character: 'broquaint' } },   {     '$group': {       _id:   { race: '$race', background: '$background'},       count: { '$sum': 1 }     }   } ) 

so question - given example collection can arrive @ desired output purely through mongodb's aggregation framework?

for might rendered many in advance!

yes, can aggregation framework. won't pretty, it'll still faster mapreduce...

here in nutshell (output different format give same content):

> group1 = {     "$group" : {         "_id" : "$race",         "class" : {             "$push" : "$class"         },         "count" : {             "$sum" : 1         }     } }; > unwind = { "$unwind" : "$class" }; > group2 = {     "$group" : {         "_id" : "$class",         "classcount" : {             "$sum" : 1         },         "races" : {             "$push" : {                 "race" : "$_id",                 "racecount" : "$count"             }         }     } }; > unwind2 = { "$unwind" : "$races" }; > group3 ={     "$group" : {         "_id" : 1,         "classes" : {             "$addtoset" : {                 "class" : "$_id",                 "classcount" : "$classcount"             }         },         "races" : {             "$addtoset" : "$races"         }     } }; > db.races.aggregate(group1, unwind, group2, unwind2, group3); {     "result" : [         {             "_id" : 1,             "classes" : [                 {                     "class" : "fighter",                     "classcount" : 1                 },                 {                     "class" : "hunter",                     "classcount" : 3                 },                 {                     "class" : "rogue",                     "classcount" : 1                 }             ],             "races" : [                 {                     "race" : "naga",                     "racecount" : 2                 },                 {                     "race" : "halfling",                     "racecount" : 3                 }             ]         }     ],     "ok" : 1 } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -