在Javascript中迭代地将数组元素传递给mapReduce

在Javascript中迭代地将数组元素传递给mapReduce,javascript,arrays,mapreduce,Javascript,Arrays,Mapreduce,我试图将关键字数组的元素传递给映射器函数。这样做的目的是统计关键字出现在文档中的数量。我得到一个错误,表示映射器函数中未定义单词数组。当我使用映射器函数emit(words[I],1)时,它不知道words[I]是什么。我如何处理这个问题?这是我的密码 db = db.getSiblingDB('hw4') //this is the database I'm working with words = ['kinda','Scott']; //these are my keywords var

我试图将关键字数组的元素传递给映射器函数。这样做的目的是统计关键字出现在文档中的数量。我得到一个错误,表示映射器函数中未定义单词数组。当我使用映射器函数emit(words[I],1)时,它不知道words[I]是什么。我如何处理这个问题?这是我的密码

db = db.getSiblingDB('hw4') //this is the database I'm working with
words = ['kinda','Scott']; //these are my keywords
var mapper = function() { //if searching for 'kinda', I want this function to emit (kinda, 1). This is not working
    emit( words[i], 1);          
};

var reducer = function(key, values) {
    return Array.sum(values) //this sums all of the ones and thereby gets the total # of docs that contain the keyword
};


for (var i=0; i<words.length;i++) { //here I'm iterating over the words array
result = db.messages.mapReduce(mapper,
                               reducer,
                               {
                                   out: { merge: 'keywordCollectiontest'}, //the results of each mapReduce are added to this collection
                                   query: {msg: {$regex: words[i]}} //this returns only the docs that contain words[i]
                               }
)
    printjson(result) //this simply shows information about how the mapReduce went
}
所以我得到的确切错误是:

"errmsg" : "exception: ReferenceError: words is not defined near 'emit( words[i], 1)'  (line 2)"

谢谢您的帮助。

这部分
var mapper=function()
肯定是错误的。您没有为该函数提供任何参数。我希望mapper使用单词数组,因此如果我将mapper更改为
函数mapper(array){emit(array[I],1);}因此,在我的mapReduce中,我将映射器部分更改为
mapper(words)
我收到一个错误,表示未定义emit。如何定义集合
消息
?在此基础上,您必须定义映射器和减速器的函数。请参阅
消息
只是
{msg:'这里有一条消息'}
,我正在使用regex搜索消息字符串中的关键字。消息不包含作为字段值的关键字,因此我需要某种方法将关键字传递到mapper。
"errmsg" : "exception: ReferenceError: words is not defined near 'emit( words[i], 1)'  (line 2)"