Skip to content

Instantly share code, notes, and snippets.

@glamp
Created September 4, 2015 16:53
Show Gist options
  • Save glamp/942a46b7b29bb447805f to your computer and use it in GitHub Desktop.
Save glamp/942a46b7b29bb447805f to your computer and use it in GitHub Desktop.

Revisions

  1. glamp created this gist Sep 4, 2015.
    28 changes: 28 additions & 0 deletions mongodb-word-count.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    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)