【发布时间】:2011-09-27 09:09:24
【问题描述】:
在 mongodb 中,我有一个 map 函数如下:
var map = function() {
emit( this.username, {count: 1, otherdata:otherdata} );
}
和reduce函数如下:
var reduce = function(key, values) {
values.forEach(function(value){
total += value.count; //note this line
}
return {count: total, otherdata: values[0].otherdata}; //please ignore otherdata
}
问题在于指出的行:
total += value.count;
在我的数据集中,reduce 函数被调用了 9 次,假设的 map 缩减结果计数应该是 8908。
使用上面的行,返回的结果将正确返回为 8908。
但如果我将这一行改为:
total += 1;
返回的结果只有 909,大约是假设结果的 1/9。
另外我尝试了 print(value.count) 并且打印的结果是 1。
什么解释了这种行为?
【问题讨论】:
-
你不需要在某处声明
total吗?