Tuesday, 14 March 2017

get count in mongoDB against an array

Need to get the count against the ArticleId from the collection in nodejs and mongodb

/* 1 */
{
    "_id" : ObjectId("5836d0f7f8462cbc6d0caffc"),
    "DeviceId" : "abcd1234",
    "AppType" : "web",
    "UserId" : "5836cb01f8462cbc6d0caff8",
    "ArticleId" : "5836cb01f8462cbc6d0caff8",
    "Timestamp" : ISODate("2016-11-24T11:37:27.851Z")
}

/* 2 */
{
    "_id" : ObjectId("5836e5f617632727286475e0"),
    "Apptype" : "android",
    "DeviceId" : "abcsd1212",
    "UserId" : "19283738264817",
    "ArticleId" : "5836wp0615555727286475e0",
    "Timestamp" : "2016-11-24 12:23:04.484Z"
}

/* 3 */
{
    "_id" : ObjectId("583d487803d7691d54a82060"),
    "DeviceId" : null,
    "AppType" : null,
    "UserId" : null,
    "ArticleId" : "583d28412f93b67c1500002a",
    "Timestamp" : ISODate("2016-11-29T09:20:56.895Z")
}

/* 4 */
{
    "_id" : ObjectId("583d4aa564b47b199ca6b71e"),
    "DeviceId" : null,
    "AppType" : null,
    "UserId" : null,
    "ArticleId" : "583559814d93b6080d000029",
    "Timestamp" : ISODate("2016-11-29T09:30:13.785Z")
}

/* 5 */
{
    "_id" : ObjectId("583d4ab364b47b199ca6b71f"),
    "DeviceId" : null,
    "AppType" : null,
    "UserId" : null,
    "ArticleId" : "58343672dc1434742700002a",
    "Timestamp" : ISODate("2016-11-29T09:30:27.200Z")
}

/* 6 */
{
    "_id" : ObjectId("584502efc0e6500558d20874"),
    "DeviceId" : null,
    "AppType" : null,
    "UserId" : null,
    "ArticleId" : "583d28412f93b67c1500002a",
    "Timestamp" : ISODate("2016-12-05T06:02:23.905Z")
}

where clause is like (collection of ArticleId)

{
"5836d0f7f8462cbc6d0caffc",
"5836e493f2acbd1d34648e78",
"5836dba8a2943528448a3050"
}

need to get the result like

[{"ArticleId" : "5836d0f7f8462cbc6d0caffc","ViewCount":5},
{"ArticleId" : "5836e493f2acbd1d34648e78","ViewCount":42},
{"ArticleId" : "5836dba8a2943528448a3050","ViewCount":19}]

Please help and please let me know if anything is unclear, thanks in advance



via Ani

No comments:

Post a Comment