title: CouchDB - aggregate multiple values in the reduce step
body: <p>I am trying to aggregate multiple values emitted from my map in a custom reduce function( rereduce enabled) but I get null values in the result except for few datapoints where I see 0s., Any help is much appreciated. Thanks</p>
  
  <p>My data emitted is in this format: </p>
  
  <pre><code>Key:"dateX" Values:"{'a':435, 'b':5645}"
  Key:"dateX" Values:"{'a':8451, 'b':9245}"
  Key:"dateX" Values:"{'a':352, 'b':450}"
  
  Key:"dateY" Values:"{'a':5675, 'b':1245}"
  Key:"dateY" Values:"{'a':4455, 'b':620}"
  </code></pre>
  
  <p>I want to aggregate/sum the values of both a &amp; b for dateX and dateY, my map-reduce is:</p>
  
  <pre><code>"map": "function(doc){emit(doc.logDate, {'a': doc.a, 'b': doc.b} );}",
  
  "reduce": "function(key, values, rereduce) { 
                      var total = {tA:0, tB:0}; 
                      if(rereduce){ 
                          for(i=0; i&lt;values.length; i++)
                          { 
                              total.tA += values[i].a; 
                              total.tB += values[i].b;
                          } 
                          return total;
                      } 
                      total.tA = sum(values.a); 
                      total.tB = sum(values.b); 
                      return total; }"
  ------------------------
  Results:
  dateX        {tA: 0, tB: 0} 
  dateY        {tA: null, tB: null}   
  dateZ        {tA: null, tB: null}
          .
          .
          .   
  </code></pre>