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: Associating users and comments
Date Thu, 02 Oct 2008 03:35:56 GMT
On Wed, Oct 1, 2008 at 8:12 PM, Ben Bangert <ben@groovie.org> wrote:
> On Oct 1, 2008, at 8:00 PM, Chris Anderson wrote:
>
>> http://wiki.apache.org/couchdb/Views
>>
>> The reduce docs are somewhat lacking...
>
>
> function(doc) {
>  if (doc.type == 'Human') {
>    emit([doc._id, 0], {type:doc.type, name:doc.name})
>  } else if (doc.type == 'Paste') {
>    emit([doc.human_id, 1], doc);
>  }
> }
>
> Now, how would I filter this list, so that I don't get all the Humans with
> zero paste's filed for them? Ie, I only want the human row in the result set
> if there's going to be a Paste type row following it.

you can see which humans have pastes with this reduce:

function(keys, values, combine) {
  if (combine) {
    return sum(values);
  } else {
    var count = 0;
    for (var i in keys) {
      count += keys[i][1];
    }
    return count;
  }
}

Then query the reduce with group_level=1. This will give you the count
of pastes per human.

>
> Alternatively, how would I have it return only 5 Paste's max for each user?
>
can't really do that in one request... a request to the map per human
with startkey=["097337c75773737022bb4e8cd3a92140"]&endkey=["097337c75773737022bb4e8cd3a92140",{}]&count=6
would do it.



-- 
Chris Anderson
http://jchris.mfdz.com

Mime
View raw message