Can you use conditional logic within your map function to initialize a value?
I'm using a mapreduce to do a report and I ran into a "doc has no properties" and I think it is because some of the document do not have the property that I am doing calculation on.
the following is my map function:
var map = function(){
emit(this.SurveyId, {count: 1, totalScore: : this.totalScore, networth: this.networth});}
reduce function:
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;}
finalize function:
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 is not in all document in SurveyReu开发者_开发技巧slt collection. I think this is why it failed. How do i specify so that if networth doesn't exist, use 0 instead? Thank you.
It's javascript...
if "networth" in item:
doc.networth+=item.networth;
精彩评论