couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sho Fukamachi <sho.fukama...@gmail.com>
Subject dual purpose filter/count views
Date Sun, 09 Nov 2008 21:36:46 GMT
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

Mime
View raw message