Can you implement document joins using CouchDB 2.0 'Mango'?

我的未来我决定 提交于 2019-12-11 01:58:43

问题


From previous work on CouchDB 1.6.1, I know that it's possible to implement document joins in a couple ways:

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

This SQL query:

SELECT [Student Name], [Course ID] FROM Students RIGHT OUTER JOIN Courses ON Students.[Student ID] = Courses.[Student ID]

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)
}

And either a group and reduce function

// 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)

Or you could use a List function to iterate through either a grouped or ungrouped Map index.

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

Question:

  1. Can you 'emulate' document joins in Mango?
  2. If you can, would this be better or worse than using MapReduce to do the same thing?

回答1:


I think you already figured it out, but jut for the records : you can use equality selectors, and put them in a OR operator.

The query should be something looking like that :

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

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.



来源:https://stackoverflow.com/questions/42206380/can-you-implement-document-joins-using-couchdb-2-0-mango

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