couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ido Ran <ido....@gmail.com>
Subject Strange Reduce Value
Date Mon, 26 Aug 2013 09:55:54 GMT
Hi.
I have CouchDB with documents that contain possible answers to survey.
All the possible answer documents have shared SurveyID field and either
have *answer* field or they do not.

I have this map function:
     function(doc) {
            if (doc.$type === 'employee') {
              var val = { yes: 0, no: 0 };
              if (doc.answer) val.yes++;
              else val.no++;
              emit(doc.surveyID, val);
            }
          }

This effectively create row with surveyID as key and structure with yes:1
or no:1 depend on if it have or does not have answer.

I then have this reduce function:
function (key, values, rereduce) {
 var r = { yes:0, no: 0, count: values.length };
 for (var i = 0; i < values.length; i++) {
   r.yes += values[i].yes;
   r.no += values[i].no;
 }
 return r;
}

The strange thing is the results (thanks for chrome for the nice colors).
The first result show 14 yes, 66 no and only 6 count ???
The second result look normal as so the third but the forth is again not
making sense with 42 yes and only 2 count.

Can someone explain to me what am I doing wrong?

Thank you,
Ido.

{

   - rows:
   [
      -
      {
         - key: "20120814132737A",
         - value:
         {
            - yes: 14,
            - no: 66,
            - count: 6
            }
         },
      -
      {
         - key: "20120821121948B",
         - value:
         {
            - yes: 4,
            - no: 4,
            - count: 8
            }
         },
      -
      {
         - key: "20120828085543C",
         - value:
         {
            - yes: 7,
            - no: 0,
            - count: 7
            }
         },
      -
      {
         - key: "20120923124200D",
         - value:
         {
            - yes: 42,
            - no: 0,
            - count: 2
            }
         },

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message