MongoDB Get the no of liked users count? -


{     "_id": objectid("531d68dab0b80560100dd205"),     "id": 47,     "userid": "2",     "content": "like check ",     "datetime": "2014/03/10",     "parent": null,     "files": [         objectid("531d68dab0b80560100dd203")     ],     "category": null,     "comments": [      ],     "type": null,     "likes": [          "4",         "4",         "6",         "12",         "1"     ],     "recipients": [         "4",         "4",         "6",         "12",      ] } 

i have collection called posts, need find out liked users count of post collection. please help. have tried below codes in mongodb terminal.

db.posts.aggregate([{$unwind : "$likes"}, {$group: { _id:null, number :{$sum :1}}}]) 

but result { "result" : [ ], "ok" : 1 }

you should write this:

db.posts.aggregate([   {$project: {"likes" :1}},   {$unwind : "$likes" },   {$group: {"_id": "$_id", count: {$sum: 1}}} ]) 

result:

{     "result" : [          {             "_id" : objectid("531d68dab0b80560100dd205"),             "count" : 5         }     ],     "ok" : 1 } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -