Say I have the following collection containing the fields username
and message
:
{
"username" : "bob",
"message" : "testing hello"
}
{
"username" : "harry",
"message" : "hello hello"
}
{
"username" : "harry",
"message" : "hi hello"
}
{
"username" : "tom",
"message" : "hello there"
}
{
"username" : "john",
"message" : "hey"
}
etc ...
I can use the following code to get the top 10 usernames for which the word "hello" occurs the most in their messages:
db.collection('collectionName').aggregate(
{ $match : { message: /hello/ } },
{ $group : { _id: '$username', count: { $sum: 1 } } },
{ $sort : { count : -1 } },
{ $limit : 10 },
function(err, results) {
console.log(results);
});
This results in something like:
{ _id: 'harry', count: 2 },
{ _id: 'bob', count: 1 },
{ _id: 'tom', count: 1 }
My question - how do I search each document (or message
field) multiple times to retrieve the total count of the word "hello", not just the number of documents which contain it at least once. The count for "harry" would then return 3 instead of 2. Thanks.