DominixZ
DominixZ

Reputation: 162

How can I display CouchDB Complex output like this in one request?

I just the beginner of couchdb so I may be misunderstand point of view so you can teach and discuss with me

Doc Type - User - Topic - Comment

Requirement - I want to webboard - 1 Request to get this complex doc

Output I need KEY "topic-id" , VALUE { _id : "topic-id", created_at:"2011-05-30 19:50:22", title:"Hello World", user: {_id : "user-1",type:"user",username:"dominixz",signature:"http://dominixz.com"} comments: [ {_id:"comment-1", text:"Comment 1",created_at:"2011-05-30 19:50:22",user: {_id : "user-1",type:"user",username:"dominixz",signature:"http://dominixz.com"}}, {_id:"comment-2", text:"Comment 2",created_at:"2011-05-30 19:50:23",user: {_id : "user-2",type:"user",username:"dominixz2",signature:"http://dominixz1.com"}}, {_id:"comment-3", text:"Comment 3",created_at:"2011-05-30 19:50:24",user: {_id : "user-3",type:"user",username:"dominixz3",signature:"http://dominixz2.com"}}, ] }

I have "user" data like this {_id:"user-1",type:"user",username:"dominixz",signature:"http://dominixz.com"} {_id:"user-2",type:"user",username:"dominixz2",signature:"http://dominixz1.com"} {_id:"user-3",type:"user",username:"dominixz3",signature:"http://dominixz2.com"}

"Topic" data like this {_id : "topic-id",created_at:"2011-05-30 19:50:22",title:"Hello World",user:"user-1"}

"Comment" data like this {_id:"comment-1",type:"comment" , text:"Comment 1", created_at:"2011-05-30 19:50:22" , user: "user-1" , topic:"topic-id"} {_id:"comment-2",type:"comment" , text:"Comment 2", created_at:"2011-05-30 19:50:23" , user: "user-2" , topic:"topic-id"} {_id:"comment-3",type:"comment" , text:"Comment 3", created_at:"2011-05-30 19:50:24" , user: "user-3" , topic:"topic-id"}

How can I write map,reduce,list for achieve this complex data ? and how about when I wanna use LIMIT , OFFSET like in db

Thank in advance

Upvotes: 1

Views: 152

Answers (2)

Dustin
Dustin

Reputation: 90980

It's a bit hard to tell what you're looking for here, but I think you're asking for a classic CouchDB join as documented in this web page.

I'd recommend reading the whole thing, but the punchline looks something like this (translated for your data):

function (doc) {
    if (doc.type === 'topic') {
        emit([doc._id, 0, doc.created_at], null);
    } else if (doc.type === 'comment') {
        emit([doc._id, 1, doc.created_at], null);
    }
}

That map will return the topic ID followed by all of its comments in chronological order. The null prevents the index from getting too large, you can always add include_docs=true on your request to pull full docs when you need them, or you can use index best practices of including the bits that are interesting there.

Upvotes: 1

Elad
Elad

Reputation: 3130

CouchDB is a document database, not a relational database. As such it is best suited to deal with documents that encompass all the related data. While you can normalize your schema relational-style like you did, I'd argue that this isn't be best use case for Couch.

If I were to design your CMS in Couch I'd keep the topic, content and comments all in a single document. That would directly solve your problem.

You're free of course to use document stores to emulate relational databases, but that's not their natural use case, which leads to questions like this one.

Upvotes: 0

Related Questions