Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass two parameter values to couchdb views?

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?

like image 739
Ajay Avatar asked Apr 21 '11 09:04

Ajay


People also ask

What is view in CouchDB?

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.

How do you create a view in CouchDB?

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.

What is the maximum number of views a document can have?

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.


1 Answers

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

like image 129
SHernandez Avatar answered Nov 12 '22 05:11

SHernandez