Mongo: count the number of word occurrences in a set of documents

前端 未结 4 804
北荒
北荒 2020-12-08 16:19

I have a set of documents in Mongo. Say:

[
    { summary:\"This is good\" },
    { summary:\"This is bad\" },
    { summary:\"Something that is neither good         


        
4条回答
  •  清歌不尽
    2020-12-08 17:03

    A basic MapReduce example

    var m = function() {
        var words = this.summary.split(" ");
        if (words) {
            for(var i=0; i

    This will insert word counts into a collection name words_count which you can sort (and index)

    Note that it doesn't use stemming, omit punctuation, handles stop words etc.

    Also note you can optimize the map function by accumulating repeating word(s) occurrences and emitting the count, not just 1

提交回复
热议问题