我正在做一个 MapReduce 过程并得到了一些奇怪的结果。预期结果是:
{ "_id" : {...}, "value" : { "register" : 1, "login" : 3, "payment" : 4.99, "transaction" : 1} }
我得到的是:
{ "_id" : {...}, "value" : { "register" : 1, "login" : 6, "payment" : 4.99, "transaction" : 0, "transcation" : NaN } }
除了“事务”之外,一切看起来都正确,第一个“事务”:0 应该是 ...:1,第二个事务:NaN 不应该存在。
我打印了所有日志,但没有发现任何问题。
代码:
exports.basic = function(){
var map = function(){
var key = {
app:this.properties.YA0token,
uuid:this.properties.distinct_id
}
var value = {
register:0,
login:0,
payment:0,
transaction:0
};
if (this.event=='register') {
value.register = 1;
} else if (this.event=='login') {
value.login = 1;
} else if (this.event=='YA0charge') {
value.payment = this.properties.amount;
value.transaction = 1;
print(value.register,value.login,value.payment,value.transaction);
}
emit(key,value);
}
var reduce = function(key,values){
var result = {
register:0,
login:0,
payment:0,
transaction:0
};
print("values.length = ",values.length);
values.forEach(function(value){
result.register += value.register;
result.login += value.login;
result.payment += value.payment;
result.transcation += value.transcation;
if (value.payment>0) print('value:',value.register,value.login,value.payment,value.transaction);
});
return result;
}
var options = {
out:{merge:"mr_users"},
query:{
"event":{$in:["register","login","YA0charge"]},
"properties.time": {"$gte": new Date("9 1 2013"), "$lt": new Date("9 3 2013")}
}
}
eventCollection.mapReduce(map,reduce,options,function (err, collection2) {
logger.info('MapReduce for basic is done.');
collection2.find().toArray(function(err,item){
logger.silly(item);
});
});
}