tags:

views:

49

answers:

1

I'm pretty new to CouchDB and I still have some problems wrapping my head around the whole MapReduce way of querying my data...

To stay with the traditional "Blog" example, let's say I have 2 types of documents: post and comment... each comment document has a post_id field...

Is there a way I can get a list of posts with the number of comments for each of these posts with only 1 query? Let's say I want to display a list of post titles with the number of comments for each post like this:

My First Post: 4 comments
My Second Post: 6 comments
....

I know I can do the following:

function(doc) {
    if(doc.type == "comment") {
        emit(doc.post_id, 1);
    }
}

and then reduce it like this:

function (key, values, rereduce) {
    return sum(values);
}

which gives me a list of each blog post id, with the number of comments for each posts. But then I need to fetch the blog posts titles separately since the only thing I have right now is their id...

So, is there a way I could retrive a list of each blog post titles, with the number of comments for each posts, by doing only 1 query?

+1  A: 

Have a look at View Collation:

http://wiki.apache.org/couchdb/View_collation?action=show&redirect=ViewCollation

cloudhead