incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul Davis" <>
Subject Re: Associating users and comments
Date Wed, 01 Oct 2008 17:21:25 GMT
On Wed, Oct 1, 2008 at 1:01 PM, Ben Bangert <> wrote:
> On Sep 30, 2008, at 7:26 PM, Paul Davis wrote:
>> 1. To prevent the need to re-update all your sub-records, use a
>> uniquely identifying piece of data that doesn't change. Ie, use the
>> user docid instead of slug. This prevents you from being able to
>> display a post without fetching the user doc, but getting the whole
>> shebang in one get should alleviate that. (as was the original
>> intent).
> Yes, I was planning on having comments reference the CouchDB assigned ID of
> the user, as well as the ID of the document its commenting on. While I
> understand that somehow a map/reduce will help me assemble this information,
> the lack of examples and docs on what exactly is going on is making it
> difficult to see how I'd actually write it.
>> 2. You're right that you the view count only gives you that many rows,
>> regardless of what type. For the first "Give me back 5 customers with
>> their orders" I'd do a reduce with group_level=1 and sum the rows,
>> then a second get to get that many rows from the view. The second
>> question about getting the latest 5 orders for each customer would
>> require a different view probably. I have the feeling it should be
>> doable with a clever emit, but I'm a bit tired so its not
>> materializing.
> I haven't seen any map/reduce examples that do such combinations of data.
> Does anyone have some pointers to map/reduce examples that return data
> mingled together like this?

I can't point at any specific docs just yet, but the thing that made
it click for me was Chris Anderson's Markov Chain example [1].

So in your view, you'd be doing something like:

    emit([doc.post_id, doc.comment_id], 1)
    emit([doc._id, 0], 1)


function(keys, values)
    return sum(values);

And then you'd get the row counts for a number of posts would be something like:


The idea here is that when you emit an array key, you can do a
group_level=blah to only reduce down to the first blah unique


> Cheers,
> Ben

View raw message