问题描述
从CouchDB 1.6.1的先前工作中,我知道可以通过两种方式实现文档联接:
From previous work on CouchDB 1.6.1, I know that it's possible to implement document joins in a couple ways:
例如,以简单的模式'students and 'courses
:
For example, with a simple schema of 'studentsand 'courses
:
// Students table
| Student ID | Student Name
| XYZ1 | Zach
// Courses table
| Course ID | Student ID
| COURSE1 | XYZ1
此SQL查询:
SELECT [Student Name], [Course ID] FROM Students RIGHT OUTER JOIN Courses ON Students.[Student ID] = Courses.[Student ID]
可以在CouchDB 1.6中使用地图功能实现
Could be implemented in CouchDB 1.6 with a map function:
// Map function
function(doc){
if (doc.type == 'Course') emit(doc["Student ID"], doc);
if (doc.type == 'Student') emit(doc["Student ID"], doc)
}
还有一组归约功能
// Group would produce:
Student ID: [{course doc}, {student doc}, {course doc}, etc]
// Reduce would allow you to then process student and course docs for a single ID (with CouchDB protesting about expanding view indexes)
或者您可以使用List函数遍历已分组或未分组的Map索引.
Or you could use a List function to iterate through either a grouped or ungrouped Map index.
在此处中查看芒果的文档,提到_find(我假设是"Mango端点")使用索引.我看不出一个字段等于另一个字段的意思" ,但是我对芒果一点都不熟悉……
Looking at documentation for Mango here, there is mention that _find (which I'm assuming is the 'Mango endpoint') uses indexes. I don't see way of saying 'where a field is equal to another field', but then I'm not very familiar with Mango at all...
问题:
- 您可以模仿"芒果中的文档联接吗?
- 如果可以的话,这比使用MapReduce做同一件事好还是坏?
推荐答案
我认为您已经弄清楚了,但是为了记录起见:您可以使用相等选择器,并将其放在OR运算符中.
I think you already figured it out, but jut for the records : you can use equality selectors, and put them in a OR operator.
查询应该是这样的:
{"studentId": "SOMEID" }
{"$or": [{"type": {"$eq": "Student"}}, {"type": {"$eq": "Course"}}]}
话虽如此,您似乎尝试使用CouchBb中的原始关系数据.就像我经常说的那样,您不能只将关系数据转储到CouchDb中并期望过上幸福的生活.在将数据存储到CouchDb中之前,您可能需要考虑将其转换为富域对象.
That being said, you seem to try to work with raw relational data in CouchBb. As I always say, you can't just dump relational data in CouchDb and expect to have a happy life. You may want to consider turning your relational data into rich domain objects before storing it in CouchDb.
这篇关于您可以使用CouchDB 2.0'Mango'实现文档联接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!