incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Anderson <jch...@apache.org>
Subject Re: Finding how many items correspond to a key in a view
Date Fri, 05 Jun 2009 20:05:41 GMT
On Fri, Jun 5, 2009 at 1:00 PM, Brian Candler<B.Candler@pobox.com> wrote:
> On Wed, Jun 03, 2009 at 10:54:19PM -0700, Nadav Samet wrote:
>> Given a key I'd like to find how many items in a permanent view correspond
>> to that key (without retrieving the items themselves, because there can be
>> too many).
>>
>> I currently achieve it by having another mapreduce just to compute this
>> count, but it sounds like there must be a better way.
>
> Sounds like the right way to me. If you have an existing view which does
>
>  emit(key,anything);
>
> and as long as this view doesn't already have a reduce function, you can add
> a counter reduce function(*) to it. Then you just have to remember to query
> it with reduce=false when you *don't* want the counts. (I'd prefer it if you
> had to say reduce=true to get the reduction, but that's probably just me :-)
>

you can also (in trunk) specify you're reduce function as:

_count

which is equivalent of Brian's reduce but in ran as native Erlang.

> Regards,
>
> Brian.
>
> (*) e.g. this one:
>
> function(ks, vs, co) {
>  if (co) {
>    return sum(vs);
>  } else {
>    return vs.length;
>  }
> }
>



-- 
Chris Anderson
http://jchrisa.net
http://couch.io

Mime
View raw message