Tuesday, 14 March 2017

get count of multiple values of an array in mongodb

I want to perform a search in mongodb and nodejs which will return the count of ids that I will provide.

my collection is a log table

{
    "_id" : ObjectId("5836d0f7f8462cbc6d0caffc"),
    "DeviceId" : "abcd1234",
    "AppType" : "web",
    "UserId" : "5836cb01f8462cbc6d0caff8",
    "ArticleId" : "5836cb01f8462cbc6d0caff8",
    "Timestamp" : ISODate("2016-11-24T11:37:27.851Z")
},
{
    "_id" : ObjectId("5836dba8a2943528448a3050"),
    "DeviceId" : null,
    "AppType" : null,
    "UserId" : null,
    "ArticleId" : null,
    "Timestamp" : ISODate("2016-11-24T12:23:04.484Z")
},
{
    "_id" : ObjectId("5836e445c3b43429b4810ad4"),
    "DeviceId" : null,
    "AppType" : null,
    "UserId" : null,
    "ArticleId" : null,
    "Timestamp" : ISODate("2016-11-24T12:59:49.820Z")
},
{
    "_id" : ObjectId("5836e493f2acbd1d34648e78"),
    "DeviceId" : null,
    "AppType" : null,
    "UserId" : null,
    "ArticleId" : null,
    "Timestamp" : ISODate("2016-11-24T13:01:07.030Z")
}

and so on...

my search string will be

{ ObjectId("5836d0f7f8462cbc6d0caffc"), 
ObjectId("5836e493f2acbd1d34648e78"), 
ObjectId("5836dba8a2943528448a3050")}

and I want a result set like below

 { 1:{_id:ObjectId("5836d0f7f8462cbc6d0caffc"), count:2}, 
   2:{ObjectId("5836e493f2acbd1d34648e78"), count:9}, 
   3:{ObjectId("5836dba8a2943528448a3050"), count:35}}

Can any one please provide me the query, thanks in advance



via Arnab Majumdar

No comments:

Post a Comment