In my database, I have records that contain various URLs, such as
https://www.youtube.com/watch?v=blablabla
.
My goal is to tally the number of URLs for each individual site. For example:
[{
site: 'youtube.com',
count: 25
},
{
site: 'facebook.com',
count: 135
}]
To achieve this, I attempted to use the following aggregation pipeline:
db.getCollection('records').aggregate([
{'$match': {'url': /.*youtube\.com.*/}}, // using youtube as an example
{'$group': {'_id': {'site': '$url', 'count': {'$sum': 1}}}},
{'$project': {'_id': false, 'site': '$_id.site', 'count': '$_id.count'}}
]);
The result of this pipeline is:
[{
"site" : "youtube.com/blablabla1",
"count" : 1.0
},
{
"site" : "youtube.com",
"count" : 1.0
},
{
"site" : "www.youtube.com/blablabla2",
"count" : 1.0
},
{
"site" : "www.youtube.com/blablabla1",
"count" : 1.0
}]
However, this method is not able to accurately count identical strings.
What could be the issue with my current approach?