mongodb - Can you use conditional logic within your map function to initialize a value? -


i'm using mapreduce report , ran "doc has no properties" , think because of document not have property doing calculation on.

the following 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 not in document in surveyreuslt collection. think why failed. how specify if networth doesn't exist, use 0 instead? thank you.

it's javascript...

if "networth" in item:     doc.networth+=item.networth; 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -