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?
Views are the primary tool used for querying and reporting on CouchDB documents. There you'll learn how they work and how to use them to build effective applications with CouchDB. 3.2.1.
There are two employees in our "employees" database. Now, Open Fauxton and go to all documents where you see a block named New View. View is created now.
There is no hard limit on the number of views. There are a few things I would recommend though: First, split up your views among many design documents. My first thought is 1 per user, but you could probably sub-divide them further depending on how many views you actually have.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With