您可以使用 CouchDB 2.0 'Mango' 实现文档联接吗?
Can you implement document joins using CouchDB 2.0 'Mango'?
根据之前关于 CouchDB 1.6.1 的工作,我知道可以通过几种方式实现文档连接:
例如,使用简单的架构 '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 中使用映射函数实现:
// 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 函数循环访问分组或未分组的地图索引。
查看 Mango here 的文档,提到 _find(我假设是 'Mango endpoint')使用索引。我看不出怎么说 'where a field is equal to another field',但我对 Mango 一点也不熟悉...
问题:
- 你能'emulate'文件加入Mango吗?
- 如果可以的话,这比使用 MapReduce 做同样的事情更好还是更糟?
我想您已经弄明白了,但只是为了记录:您可以使用相等选择器,并将它们放在 OR 运算符中。
查询应该是这样的:
{"studentId": "SOMEID" }
{"$or": [{"type": {"$eq": "Student"}}, {"type": {"$eq": "Course"}}]}
话虽如此,您似乎尝试在 CouchBb 中使用原始关系数据。正如我常说的,您不能只是将关系数据转储到 CouchDb 中并期望过上幸福的生活。在将关系数据存储到 CouchDb 之前,您可能需要考虑将其转换为丰富的域对象。
根据之前关于 CouchDB 1.6.1 的工作,我知道可以通过几种方式实现文档连接:
例如,使用简单的架构 '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 中使用映射函数实现:
// 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 函数循环访问分组或未分组的地图索引。
查看 Mango here 的文档,提到 _find(我假设是 'Mango endpoint')使用索引。我看不出怎么说 'where a field is equal to another field',但我对 Mango 一点也不熟悉...
问题:
- 你能'emulate'文件加入Mango吗?
- 如果可以的话,这比使用 MapReduce 做同样的事情更好还是更糟?
我想您已经弄明白了,但只是为了记录:您可以使用相等选择器,并将它们放在 OR 运算符中。
查询应该是这样的:
{"studentId": "SOMEID" }
{"$or": [{"type": {"$eq": "Student"}}, {"type": {"$eq": "Course"}}]}
话虽如此,您似乎尝试在 CouchBb 中使用原始关系数据。正如我常说的,您不能只是将关系数据转储到 CouchDb 中并期望过上幸福的生活。在将关系数据存储到 CouchDb 之前,您可能需要考虑将其转换为丰富的域对象。