You can join on related documents within the same database (see CouchDB documentation topic: Joins With Views - Linked Documents).
The documents from the question:
{ "docType": "school", "_id": "school1", "state": "CA" }
{ "docType": "teacher", "_id": "teacher1", "age": "40", "school": "school1" }
For this create a view, which allows filtering on the main document. The view has a map function which does the join:
function (doc) {
if (doc.school && doc.age === 40) {
emit([ doc._id, doc.age ], { _id: doc.school });
}
}
Note the filter on the age field of the teacher document.
This view is to be queried using the parameter include_docs=true (this parameter is required to get the joined document details), for example:
curl -X GET http://usr:<pwd>@127.0.0.1:5984/my_db/_design/my_design/_view/my_view?include_docs=true
For example, the output:
{
"total_rows":1,
"offset":0,
"rows":[
{
"id":"teacher1",
"key":[
"teacher1",
"40"
],
"value":{
"_id":"school11"
},
"doc":{
"_id":"school1",
"_rev":"1-0be327d3b2c9d07a4f143d93067b46c6",
"docType":"school",
"state":"CA"
}
}
]
}
This JSON result can be further processed / filtered for the schools in the state of "CA" (in an application).