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:
- Can you 'emulate' document joins in Mango?
- If you can, would this be better or worse than using MapReduce to do the same thing?
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 :
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.