三重加入CouchDB? [英] Triple join in CouchDB?
问题描述
我有三种类型的文档:
- 问题
- 用户-包含源字段
- 答案-包含相应的问题ID和用户ID
每个问题均由多个答案回答用户和每个用户仅回答一次每个问题。我想为每个问题查找源 source1的用户回答了多少个答案。
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".
推荐答案
我认为您可以使用以下方法(使用链接的文档)来获得所需的内容。
I think that the nearer that you can arrive to what you want is the following (using Linked documents).
假设您有
{ "_id": "user1", "source": "source1" },
{ "_id": "user2", "source": "source2" },
{ "_id": "answer1", "question": "question1", "user": "user1" },
{ "_id": "answer2", "question": "question1", "user": "user2" }
,然后定义以下视图
function(doc) {
if (doc.question) {
emit(doc.question, {_id: doc.user});
}
}
然后,如果您使用key = question1 ,并使用include_docs = true
,它将向您显示问题1的所有答案以及所有用户信息,而您只需要选择那些带有source = source1的答案。
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"}}
]}
但是AFAIK,您无法在视图的地图功能中使用其他文档中的信息。
But AFAIK, what you cannot do in the map function of a view is to use information from other documents.
这篇关于三重加入CouchDB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!