我創建了一個MapReduce工作下列數據結構:的MongoDB - MapReduce的敲定創建NaN值
{ "_id" : 1), "docid" : 119428, "term" : 5068, "score" : 0.198 }
{ "_id" : 2), "docid" : 154690, "term" : 5068, "score" : 0.21 }
{ "_id" : 3), "docid" : 156278, "term" : 5068, "score" : 0.128 }
{ "_id" : 4), "docid" : 700, "term" : "fire", "score" : 0.058 }
{ "_id" : 5), "docid" : 857, "term" : "fire", "score" : 0.133 }
{ "_id" : 6), "docid" : 900, "term" : "fire", "score" : 0.191 }
{ "_id" : 7), "docid" : 902, "term" : "fire", "score" : 0.047 }
我想GROUP BY術語,然後計算平均分。
這是我的MapReduce功能:
db.keywords.mapReduce(
function(){
emit(this.term, this.score);
},
function(key, values) {
rv = { cnt : 0, scoresum : 0};
rv.cnt = values.length; rv.scoresum = Array.sum(values);
return rv;
},
{
out: "mr_test" ,
finalize: function(key, reduceVal) {
reduceVal.avg = reduceVal.scoresum/reduceVal.cnt;
return reduceVal;
}
}
)
一些計算的值是正確的:
{ "_id" : 5068, "value" : { "cnt" : 5, "scoresum" : 0.887, "avg" : 0.1774 } }
但其他人創造一些奇怪的結構:
{ "_id" : "fire", "value" : { "cnt" : 333, "scoresum" : "[object
BSON][object BSON]0.176[object BSON]0.1010.181[object BSON][object .....BSON]
[object BSON][object BSON]0.1910.1710.2010.363[object BSON][object BSON]", "avg" : NaN } }
什麼是錯的我MapReduce函數?