2

从之前关于 CouchDB 1.6.1 的工作中,我知道可以通过以下几种方式实现文档连接:

例如,使用 'students 的简单模式and '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 中使用 map 函数实现:

// Map function
function(doc){
    if (doc.type == 'Course') emit(doc["Student ID"], doc);
    if (doc.type == 'Student') emit(doc["Student ID"], doc)
}

以及一个 group 和 reduce 函数

// 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 索引。

在这里查看 Mango 的文档,提到 _find(我假设它是“Mango 端点”)使用索引。我看不到“一个领域等于另一个领域”的说法,但是我对芒果一点也不熟悉......

问题:

  1. 你能在 Mango 中“模拟”文档连接吗?
  2. 如果可以,这会比使用 MapReduce 做同样的事情更好还是更差?
4

1 回答 1

1

我想您已经弄清楚了,但只是为了记录:您可以使用相等选择器,并将它们放在 OR 运算符中。

查询应该是这样的:

{"studentId": "SOMEID" }
{"$or": [{"type": {"$eq": "Student"}}, {"type": {"$eq": "Course"}}]}

话虽如此,您似乎尝试在 CouchBb 中处理原始关系数据。正如我常说的,你不能只在 CouchDb 中转储关系数据并期望过上幸福的生活。在将关系数据存储到 CouchDb 之前,您可能需要考虑将其转换为丰富的域对象。

于 2017-04-10T10:42:15.553 回答