Hi all,

This is probably a really simple question, but for some reason* I can't figure it out.

(* reason is most like extreme dumbness on my part)

I've got a bunch of views where i have something like this:

get_all_by_field:

map: if (doc.type == 'my_doc') emit(doc.field, doc);

count_all_by_field:

map: if (doc.type == 'my_doc') emit(doc.field, 1);
reduce: function(keys, values) { return sum(values) }

That works fine, but seems like it could be more elegant ..

I thought I'd be "clever" and try to combine the two, so I could get the "search" output by calling the map alone, and then getting the count by turning on reduce, something like this:

map: function(doc) {
  if (doc.type == 'my_doc') emit(doc.field, [doc, 1]);
    }
reduce: function(keys, values) { return sum(values[1]) }

The idea being that if i turn off reduce, it outputs the docs I want, but if I turn it on, I can just count the second element in the emitted array.

But for some reason it doesn't work. I've tried the above, just returning values[1], trying to sum values[0][1], etc. I either get errors or nothing.

This is probably some really simple thing that I'm missing in my tired state. Can anyone give me a hint as to what it is? : )

thanks!

Sho

Reply via email to