mongodb - display Count of multiple keys in single mongo query -


i have got mongo collection have following type of data:

{ "_id" : objectid("55d5b739f067897648000007"), "did" : "devicea","key" : "key1"} 

i need count of each key appeared device. have written following aggregate query:

db.my_collection.aggregate([{'$group':{'_id':{'deviceid':"$did",'keyis':"$key"},'count':{'$sum':1}}}]) 

i following result:

{ "_id" : { "deviceid" : "devicea", "keyis" : "key1" }, "count" : 1 } { "_id" : { "deviceid" : "devicea", "keyis" : "key2" }, "count" : 3 } { "_id" : { "deviceid" : "deviceb", "keyis" : "key1" }, "count" : 1 } { "_id" : { "deviceid" : "deviceb", "keyis" : "key3" }, "count" : 1 } { "_id" : { "deviceid" : "deviceb", "keyis" : "key4" }, "count" : 1 } { "_id" : { "deviceid" : "devicec", "keyis" : "key2" }, "count" : 1 } { "_id" : { "deviceid" : "devicec", "keyis" : "key3" }, "count" : 2 } 

what want this:

{ "_id" : { "deviceid" : "devicea"}, "count_key1" : 1, "count_key2": 3, "count_key3": 0, "count_key4": 0 } { "_id" : { "deviceid" : "deviceb"}, "count_key1" : 1, "count_key2": 0, "count_key3": 1, "count_key4": 1 } { "_id" : { "deviceid" : "devicec"}, "count_key1" : 0, "count_key2": 1, "count_key3": 2, "count_key4": 0 } 

is possible in single mongo query??

any appreciated.

there no direct way .but can have work around this.

i have created small subset of data yours (sorry bad formatting):

 > db.q.find()    { "_id" : objectid("55d5b739f067897648000007"), "did" : "devicea", "key" : "key1" }    { "_id" : objectid("55d5b739f067897648000008"), "did" : "deviceb", "key" : "key1" }    { "_id" : objectid("55d5b739f067897648000009"), "did" : "deviceb", "key" : "key2" }    { "_id" : objectid("55d5b739f067897648000019"), "did" : "devicea", "key" : "key2" }    { "_id" : objectid("55d5b739f067897648000201"), "did" : "devicea", "key" : "key2" } 

below query give result wanted :

db.q.aggregate([{ '$group':{'_id':{'deviceid':"$did",'keyis':"$key"}, 'count':{'$sum':1}}},{'$group':{'_id':{'deviceid':'$_id.deviceid'}, 'counts':{'$push':{'name':'$_id.keyis','count':'$count'}}}}, {'$unwind':'$counts'},{ "$project" : {     "countkey1" : {         "$cond" : [             {                 "$eq" : [                     "key1",                     "$counts.name"                 ]             },             "$counts.count",             0         ]     },"countkey2" : {         "$cond" : [             {                 "$eq" : [                     "key2",                     "$counts.name"                 ]             },             "$counts.count",             0         ]     }}},{ "$group" : {     "_id" : "$_id",     "countkey1" : {         "$max" : "$countkey1"     },     "countkey2" : {         "$max" : "$countkey2"     } } }]) 

//result :

{ "_id" : { "deviceid" : "devicea" }, "countkey1" : 1, "countkey2" : 2 } { "_id" : { "deviceid" : "deviceb" }, "countkey1" : 1, "countkey2" : 1 } 

here i'm assumed have limited set of keys , values known @ time of writing query.

for more details on : blog post on dynamic field names asya


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -