我正在使用mapreduce来做报告而我遇到了“doc没有属性”,我认为这是因为某些文档没有我正在进行计算的属性。
以下是我的地图功能:
var map = function(){
emit(this.SurveyId, {count: 1, totalScore: : this.totalScore, networth: this.networth});}
减少功能:
var reduce = function(key,values){
var doc = {count: 0, totalScore: 0, networth: 0};
values.forEach(function(item){
doc.count+=1;
doc.totalScore+=item.totalScore;
doc.networth+=item.networth;});
return doc;}
完成功能:
var finalize = function(key,reduced){
reduced.avg = reduced.totalScore/reduced.count;
reduced.netAvg = reduced.networth/reduced.count;
return reduced;}
map reduce call
db.SurveyResult.mapReduce(map,reduce,{finalize: finalize, out: 'my_result'})
networth不在SurveyReuslt集合的所有文档中。我想这就是它失败的原因。如何指定如果networth不存在,请使用0代替?谢谢。
答案 0 :(得分:0)
这是javascript ...
if "networth" in item:
doc.networth+=item.networth;