Created
September 4, 2015 16:53
-
-
Save glamp/942a46b7b29bb447805f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var map = function() { | |
var summary = this.message_soup_text; | |
if (summary) { | |
// quick lowercase to normalize per your requirements | |
summary = summary.toLowerCase().split(" "); | |
for (var i = summary.length - 1; i >= 0; i--) { | |
// might want to remove punctuation, etc. here | |
if (summary[i]) { // make sure there's something | |
emit(summary[i], 1); // store a 1 for each word | |
} | |
} | |
} | |
}; | |
var reduce = function( key, values ) { | |
var count = 0; | |
values.forEach(function(v) { | |
count +=v; | |
}); | |
return count; | |
} | |
// full thing | |
// db.support_emails.mapReduce(map, reduce, { out: "word_count" }) | |
// subset | |
db.support_emails.mapReduce(map, reduce, {limit: 1000, out: "word_count" }) | |
db.word_count.find().sort({ value: -1 }).limit(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/questions/16174591/mongo-count-the-number-of-word-occurences-in-a-set-of-documents