couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tom Sante <tom.sa...@gmail.com>
Subject Reduce function results
Date Thu, 20 May 2010 11:38:55 GMT
Hi,

I have a reduce function like this:
// reduce function
function (keys, values, rereduce) {
     var res={};

     if(!rereduce){ /* reduce */
	for(var val in values){
	    for(var v_n in values[val]){
		var v = new Number(values[val][v_n]);
		if(res.hasOwnProperty(v_n)){
		    res[v_n].mi = Math.min(v,res[v_n].mi);
		    res[v_n].ma = Math.max(v,res[v_n].ma);
		    res[v_n].c++;
		    res[v_n].t += v;
		}else{
		    res[v_n]={'mi':v,'ma':v,'c':1,'t':v};
		}
	    }
	}
     }else{ /* rereduce */
	for(var val in values){
	    for(var v_n in values[val]){
		if(res.hasOwnProperty(v_n)){
		    res[v_n].mi = Math.min(values[val][v_n].mi,res[v_n].mi);
		    res[v_n].ma = Math.max(values[val][v_n].ma,res[v_n].ma);
		    res[v_n].c += values[val][v_n].c;
		    res[v_n].t += values[val][v_n].t;
		}else{
		 
res[v_n]={'mi':values[val][v_n].mi,'ma':values[val][v_n].ma,'c':values[val][v_n].c,'t':values[val][v_n].t};
		}
	    }
	}
     }

     return res;
}

and it get key, values pair from a map functions that produces these:
"01:00:000074" -> {raw: "-0.213"}
"01:00:000084" -> {raw: "0.129"}

But when I query the view i get this back:
"01:00:000074" -> {raw: {mi: {}, ma: {}, c: 1, t: {}}}
"01:00:000084" -> {raw: {mi: {}, ma: {}, c: 1, t: {}}}

So for some reason if there is only 1 element to be reduced, on the line of
res[v_n]={'mi':v,'ma':v,'c':1,'t':v};
v becomes v={} and not the actual number. Any ideas why?

I tested the reduce function with the command line 'js' and send it the 
above test keys manually as function argument and then it does result in 
the correct return:
"01:00:000074" -> {raw: {mi: -0.213, ma: -0.213, c: 1, t: -0.213}}
"01:00:000084" -> {raw: {mi: -0.213, ma: -0.213, c: 1, t: -0.213}}



Mime
View raw message