Triple join in CouchDB?

╄→гoц情女王★ 提交于 2019-12-13 18:47:07

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/25684590/triple-join-in-couchdb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!