I think what I am trying to do here may be impossible, but this is worth a shot.
I have two doc types, Operator and Event.
In the example below, I am emitting 3 keys.
[place_id, operator_id, zone_name]
operator_id and zone_name are the common fields in both documents. So that is how I want to do the join between the two document types.
What I want to get back is the place_id as the first key from the first emit(), and the Event doc._id from the second emit().
That way, I can specific the startkey as the place_id I want to get back a list of Event._ids for.
function(doc) {
if(doc.doc_type == 'Operator') {
for(var i in doc.zones) {
for(var ii in doc.zones[i].origs) {
if(doc.zones[i].origs[i])
emit([doc.zones[i].origs[i], doc._id, doc.zones[i].name], null);
}
}
} else if (doc.doc_type == 'Event') {
for(var i in doc.rates) {
if(doc.rates[i].zone) {
emit([0, doc.operator_id, doc.rates[i].zone], doc._id);
}
}
}
}
The problem is the join wont work with that first value of 0 in the second emit();
I have tried emiting the keys [operator_id, zone_name] which allows the join to work, but the reduce() function takes to long and throws an error since the list of Event._ids grows to long.
I probably will have to break this up into two separate queries but hope to learn a different approach that I am missing.
Everything from
emit()is sorted by the key. Often, keys are arrays to get a nice sort order: the first element has highest priority, followed by the second element, etc. However even if the key is an array, it is still one thing and the whole view is sorted by that key.Therefore, to join between different documents, they have to sort adjacent to each other in the view. That means they should share their leftmost elements of the
emit()key.P.S. It is probably not helpful to talk about “JOIN”s with CouchDB, although people sometimes do use the word.