That is not enough because it will not tell me how many items I have.
I think I didn't take rereduce into account.
When I change the reduce method to take rereduce into account it is now
working great.
function (key, values, rereduce) {
var r = { yes:0, no: 0, count: 0 };
for (var i = 0; i < values.length; i++) {
r.yes += values[i].yes;
r.no += values[i].no;
}
r.count = r.yes + r.no;
return r;
}
I can actually drop the count property and calc it inside the application.
Thank you,
Ido
On Mon, Aug 26, 2013 at 12:10 PM, Robert Newson <[email protected]> wrote:
> Sure, you're not accumulating your count value, you just overwrite it
> with the length of the latest values array.
>
> I suggest this instead;
>
> map: function(doc) {
> if (doc.$type === 'employee') {
> emit(doc.surveyID, [doc.answer, 1]);
> }
> }
>
> reduce:: "_sum"
>
>
> The answer will be an array where the first element is the 'yes'
> votes, the second element is the total number of answers, and you can
> calculate the no votes trivially.
>
> Alternately;
>
> emit(doc.surveyID, [doc.answer ? 1 : 0, !doc.answer ? 1 : 0, 1]);
>
> will give you yes count, no count, total.
>
>
> On 26 August 2013 10:55, Ido Ran <[email protected]> wrote:
> > 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
> > }
> > },
>