2

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

3
  • Have you tried ? Something like match using in operator and group by article id while calculating the sum Commented Mar 14, 2017 at 10:48
  • Are you sure those _ids actually match the ArticleId field? The first one, "5836d0f7f8462cbc6d0caffc", is in your data as the _id of the first document. If each of them is a document _id, then each will come back with a count of 1 at most. Commented Mar 14, 2017 at 10:58
  • I have posted some dummy data here and my collection can contain more than 1 lakh data. Commented Mar 14, 2017 at 11:46

1 Answer 1

1

This problem can at best be solved by using MongoDB's aggregation feature.

The documentation is available here

What you should do is : 1) aggregate (= group) the documents by article id and then 2) count the number of documents per group.

db.collection_name.aggregate(
   [
      {
        $group : {
           _id : { articleId: $ArticleId },
           count: { $sum: 1 }
        }
      }
   ]
)

(code untested, but good start nevertheless)

Now, if you only need the count for a certain set of articles, you can filter beforehand, but depending on your collection size that might not even be necessary.

Sign up to request clarification or add additional context in comments.

2 Comments

@ani if this was helpful, please mark as correct answer. Thanks !
What's the issue ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.