incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul Davis" <paul.joseph.da...@gmail.com>
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 <ben@groovie.org> 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:

function(doc)
{
  if(doc.post_id)
    emit([doc.post_id, doc.comment_id], 1)
  else
    emit([doc._id, 0], 1)
}

Reduce:

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

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

http://localhost:5984/db_name/_view/my_reduce_view?group_level=1&startkey=[foo]&endkey=[bar]

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
elements.

[1] http://jchris.mfdz.com/code/2008/6/markov_chains_using_couchdb_s_g

> Cheers,
> Ben

Mime
View raw message