I want to reproduce this SQL in CouchDB views.
SELECT name,department FROM Persons where id = ? and group_id = ? ;
How do I write a view and query view in CouchDB for this SQL?
You could write a view like this one:
function(doc) {
if (doc.person_id && doc.group_id) {
emit([doc.person_id, doc.group_id], {"name":doc.name,"department":doc.department});
}
}
I changed your id to person_id so it cannot be easily confused with _id
I used an array as key for the view, so you can easily query it like this:
http://127.0.0.1:5984/testdb/_design/designdoc/_view/testview?key=[12,3]
This would be more or less like this query:
SELECT name, department FROM Persons where person_id = 12 and group_id = 3 ;
Here is an article about filtering and ordering with views: http://barkingiguana.com/2009/01/22/filtering-and-ordering-couchdb-view-results/
There is a very good page in the couchdb wiki on views: http://wiki.apache.org/couchdb/HTTP_view_API
This chapter in the couchdb guide explains array keys: http://guide.couchdb.org/draft/views.html