Bob
Bob

Reputation: 571

Triple join in CouchDB?

I have three type of documents:

  1. Question
  2. User - contains a source field
  3. Answer - contains the corresponding question ID and user ID

Each questions is answered by multiple users and each user answers each question only once. I want to find for every question how many answers are there answered by users of source "source1".

Upvotes: 1

Views: 519

Answers (2)

chris
chris

Reputation: 1

you can't achieve this within couchdb and need to use thirt-party modules.

for example that one:

sites.google.com/site/nosqldatajoiner/

or google nosql datajoiner

Upvotes: 0

DaniCE
DaniCE

Reputation: 2421

I think that the nearer that you can arrive to what you want is the following (using Linked documents).

Suppose you have

{ "_id": "user1", "source": "source1" },
{ "_id": "user2", "source": "source2" },
{ "_id": "answer1", "question": "question1", "user": "user1" },
{ "_id": "answer2", "question": "question1", "user": "user2" }

and you define the following view

function(doc) {
  if (doc.question) {
    emit(doc.question, {_id: doc.user});
  }
}

Then if you query that view with key="question1" and with include_docs=true it will show you all the answers to question1 with all the user information, and you will only have to select those with source = "source1".

For example, with the previous values it will return:

{"total_rows":2,"offset":0,"rows":[
{"id":"answer1","key":"question1","value":{"_id":"user1"},"doc":{"_id":"user1","_rev":"1-c99dc8987841c25c72081a84252793a0","source":"source1"}},
{"id":"answer2","key":"question1","value":{"_id":"user2"},"doc":{"_id":"user2","_rev":"1-0d44e9f4d3806fb932b1b4fcb1e1507b","source":"source2"}}
]}

But AFAIK, what you cannot do in the map function of a view is to use information from other documents.

Upvotes: 2

Related Questions