mongodb - Mongo aggregate with multiple aggregation types -


i need aggregate following data

- country: one, car: volvo, name: smith, price: 100 - country: one, car: bmw,   name: smith, price: 200 - country: two, car: romeo, name: joe,   price: 50 - country: two, car: kia,   name: joe,   price: 110 - country: two, car: kia,   name: joe,   price: 90 

(names unique, each 1 owns cars in single country)

the results, expect (pluralization not required):

- name: smith, type: volvos, country: one, val: 1   // count of car-type - name: smith, type: bmws,   country: one, val: 1 - name: smith, type: total,  country: one, val: 2   // count of cars - name: smith, type: price,  country: one, val: 300 // total car price - name: joe,   type: romeos, country: two, val: 1  - name: joe,   type: kias,   country: two, val: 2 - name: joe,   type: total,  country: two, val: 3 - name: joe,   type: price,  country: two, val: 250 

e.g. pivotized data version build report

country | name   | volvos | bmws | romeos | kias | total | price  ---------------------------------------------------------------- 1     | smith  |      1 |    1 |        |      |     2 |   300 ---------------------------------------------------------------- 2     | joe    |        |      |      1 |    2 |     3 |   250         | other  |      ? |    ? |     ... etc 

i'm thinking if aggregation framework in mongo can handle this, or should go hardcore map-reduce?

not result prescribe, in kind of mongodb way:

db.cars.aggregate([    { "$group": {        "_id": {            "name": "$name",            "type": "$car"        },        "country": { "$first": "$country" },        "carcount": { "$sum": 1 },        "totalprice": { "$sum": "$price" }    }},    { "$group": {        "_id": "$_id.name",        "cars": {            "$push": {                "type": "$_id.type",                "country": "$country",                "carcount": "$carcount",                "totalprice": "$totalprice"             }         },         "totalprice": { "$sum": "$totalprice" }    }} ]) 

which gives you:

{     "_id" : "smith",     "cars" : [             {                     "type" : "bmw",                     "country" : "one",                     "carcount" : 1,                     "totalprice" : 200             },             {                     "type" : "volvo",                     "country" : "one",                     "carcount" : 1,                     "totalprice" : 100             }     ],     "totalprice" : 300 } {     "_id" : "joe",     "cars" : [             {                     "type" : "kia",                     "country" : "two",                     "carcount" : 2,                     "totalprice" : 200             },             {                     "type" : "romeo",                     "country" : "two",                     "carcount" : 1,                     "totalprice" : 50             }     ],     "totalprice" : 250 } 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -