mongodb - Total values from all keys in subdocument -


i have mongodb collection documents like:

{     '_id': 'doc1',     'store_a': {'apples': 50, 'oranges':20},     'store_b': {'oranges': 15} } {     '_id': 'doc2',     'store_a': {'oranges':10},     'store_b': {'apples': 15} } 

how can write aggregation command give me total number of fruits each store across documents in collection without enumerating allowed kinds of fruit?

the result should like:

{     '_id': 'result',     'store_a_total': {'apples': 50, 'oranges': 30},     'store_b_total': {'apples': 15, 'oranges': 15} } 

this query works, fruit types must specified explicitly:

db.collection.aggregate( {'$group': {'_id': 'result',     'store_a_apples': {'$sum': '$store_a.apples'},     'store_a_oranges': {'$sum': '$store_a.oranges'},     'store_b_apples': {'$sum': '$store_b.apples'},     'store_b_oranges': {'$sum': '$store_b.oranges'} }}, {'$project': {     'store_a': {'apples': '$store_a_apples','oranges': '$store_a_oranges'},     'store_b': {'apples': '$store_b_apples','oranges': '$store_b_oranges'} }}) 

is there better way structure these documents facilitate type of query?

there isn't way in mongodb aggregation framework treating key inside of document data can examine or manipulate. workaround turn you're using keys here (e.g. fruit type , store name) values this:

{     "_id" : "doc1",     "stores":[         {             // store name value             "name":"store_a",             "inventory": [             {                 // fruit type                 "type" : "apple",                 "count" : 50             },             {                 "type" : "orange",                 "count" : 20             }             ]         },         {             "name": "store_b",             "inventory": [             {                 "type" : "orange",                 "count" : 15             }             ]         }     ] } 

this allows work these data more in aggregation:

db.coll.aggregate([     // split documents store name     {$unwind:"$stores"},     // split documents further fruit type     {$unwind:"$stores.inventory"},     // group documents store/fruit type, count quantities of fruit     {$group:{"_id":{"store":"$stores.name", "fruit":"$stores.inventory.type"},              "count":{$sum:"$stores.inventory.count"}}},     // reformat data more specification     {$project:{         "store":"$_id.store",         "fruit":"$_id.fruit",         "_id":0,         "count":1}}]) 

the output looks like:

{     "result" : [         {             "count" : 15,             "store" : "store_b",             "fruit" : "apple"         },         {             "count" : 15,             "store" : "store_b",             "fruit" : "orange"         },         {             "count" : 30,             "store" : "store_a",             "fruit" : "orange"         },         {             "count" : 50,             "store" : "store_a",             "fruit" : "apple"         }     ],     "ok" : 1 } 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -