Mongodb mapreduce混乱

时间:2013-08-17 06:44:35

标签: mongodb mapreduce mongodb-query

我有一个包含以下记录的集合:

{ "_id" : "279771168740729_100208116788436_242", "user_likes" : false, "message" : "nice work,nice bank", "like_count" : 4, "page_username" : "icicibank", "page_id" : "279771168740729", "can_remove" : false, "from" : { "id" : "100003762913358", "name" : "Ramakant Mirewad" }, "page_name" : "ICICI Bank", "post_id" : "279771168740729_100208116788436", "created_time" : "2012-06-06T15:40:33+0000" }
{ "_id" : "279771168740729_100208116788436_250", "user_likes" : false, "message" : "Best bank of india", "like_count" : 4, "page_username" : "icicibank", "page_id" : "279771168740729", "can_remove" : false, "from" : { "id" : "100003520362950", "name" : "Santosh Pandey" }, "page_name" : "ICICI Bank", "post_id" : "279771168740729_100208116788436", "created_time" : "2012-06-06T15:48:45+0000" }

我的目标是计算邮件中关键字“最佳”的出现次数。这里,消息只能包含“最佳”或可以包含具有“最佳”的句子。因此,我写了以下内容:

var mapFunction = function() {

    var keyword = "Best";
    var messageStr = this.message;

    if(messageStr.indexOf(keyword) != -1){
    emit(keyword, 1);
    }

};

var reduceFuntion = function(keyword, keywordCountCollection) {

    return Array.sum(keywordCountCollection);
};


db.icici_facebook.mapReduce( mapFunction,reduceFuntion,{out : "icici_fb_keyword_count", verbose : true})

我收到了一个错误:

Sat Aug 17 12:10:25.362 JavaScript execution failed: map reduce failed:{
        "errmsg" : "exception: JavaScript execution failed: TypeError: Cannot ca
ll method 'indexOf' of undefined near 'essageStr.indexOf(keyword) != -1)'  (line
 6)",
        "code" : 16722,
        "ok" : 0
} at src/mongo/shell/collection.js:L970

我也尝试过match()等,但我想我错过了一些因为js功能无法识别的东西 - 我该怎么办?

1 个答案:

答案 0 :(得分:2)

您的问题纯粹是java脚本代码,或者您不检查文档是否包含消息字段:

if(messageStr.indexOf(keyword) != -1){
    emit(keyword, 1);
}

应该是

if(messageStr  !=  null && messageStr.indexOf(keyword) != -1){
    emit(keyword, 1);
}

无论如何,使用查询您的目标会更简单:

db.icici_facebook.count({message : /best/i})