4

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.

Joey Morani
  • 25,431
  • 32
  • 84
  • 131
  • 2
    I think that the same question solved using MapReduce [here][1] [1]: http://stackoverflow.com/questions/16174591/mongo-count-the-number-of-word-occurences-in-a-set-of-documents – Muhammad Nagy May 21 '15 at 20:52
  • Wow.. I did not know you can do something like that with MongoDB. Thanks for linking that stack! – azium May 21 '15 at 21:01

0 Answers0