First we login to CouchDB and generate the auth_session token for making requests.
hash = Couchdb.login(username = 'samson',password ='sam123') auth_session = hash["AuthSession"]
To Query a permanent view
view = { :database => "contacts", :design_doc => 'my_views', :view => 'get_female_contacts'} Couchdb.find view,auth_session # => [{"_id"=>"Mary", "_rev"=>"5-bfcd67fd17dbb6a875af8f6dc497b15f", "firstname"=>"Mary", "lastname"=>"smith", # "phone"=>"212-234-1234", "email"=>"mary@mail.com", "gender"=>"female"}, # {"_id"=>"Nancy", "_rev"=>"1-d15a83d2a23b495c19df2595b636ecc8", "firstname"=>"Nancy", "lastname"=>"Lee", # "phone"=>"347-808-3734", "email"=>"nancy@mail.com", "gender"=>"female"}]
This is similar to sending a
GET http://127.0.0.1:5984/[database]/_design/[design_doc]/_view/[view_name]
For the above example
GET http://127.0.0.1:5984/contacts/_design/my_views/_view/get_female_contacts
Leanback parses the native JSON results to return only the data values.
Query a view by key
view = { :database => "contacts", :design_doc => 'the_view', :view => 'age'} age = "36" Couchdb.find(view,auth_session,key = age) # => [{"_id"=>"Nancy", "_rev"=>"2-4404d0a5a1a3dff103fd46faf1e46c30", "firstname"=>"Nancy", "lastname"=>"Lee", # "phone"=>"347-808-3734", "email"=>"nancy@mail.com", "gender"=>"female", "age"=>"36"}]
The above example will return all contacts with age = 36.
This is similar to sending a
GET http://127.0.0.1:5984/[database]/_design/[design_doc]/_view/[view_name]?key="searchterm"
For the above example
GET http://127.0.0.1:5984/contacts/_design/the_view/_view/age?key="36"
Leanback parses the native JSON results to return only the data values.
If you are running an admin party (with no admins in CouchDB), pass the auth_session as an empty string.
view = { :database => "contacts", :design_doc => 'the_view', :view => 'age'} age = "36" Couchdb.find(view,"",key = age)
Query Options
A set of options can be used to manipulate the query results.
limit
Example to limit the number of results from a query:
view = { :database => "contacts", :design_doc => 'the_view', :view => 'age'} age = "36" Couchdb.find(view,auth_session,key = age,options = {:limit => 10})
This will limit the results to 10.
view = { :database => "contacts", :design_doc => 'my_views', :view => 'get_female_contacts'} Couchdb.find view,auth_session,key=nil, options = {:limit => 12}
This will limit the results to 12.
This does the GET request below:
GET /contacts/_design/my_views/_view/get_female_contacts?limit=12
skip
To skip documents:
view = { :database => "contacts", :design_doc => 'my_views', :view => 'get_female_contacts'} Couchdb.find view,auth_session,key=nil, options = {:skip => 20}
This will skip the first 20 documents. This does the GET request below:
GET /contacts/_design/my_views/_view/get_female_contacts?skip=20
Note that skip is considered a relatively expensive operation for CouchDB.
descending
To return the view results in descending order (couchDB returns results in ascending order by default.) use options={:descending => true}
Example:
view = { :database => "contacts", :design_doc => 'my_views', :view => 'get_female_contacts'} Couchdb.find view,auth_session,key=nil, options = {:descending => true}
This will be a GET request:
GET /contacts/_design/my_views/_view/get_female_contacts?descending=true
startkey & endkey
To return a range of results using couchDB’s startkey & endkey, for example to query an employee database for employees within a salary range:
view = { :database => "employees", :design_doc => 'salary_finder', :view => 'find_by_salary'} options = {:startkey => "53000", :endkey => "99000"} Couchdb.find view, auth_session,key=nil, options
This will return all employees within the salary range of 53000 to 99000.
This does a GET request to:
GET /employees/_design/salary_finder/_view/find_by_salary?startkey=53000&endkey=99000
To return all employees with salary 99000 or less:
view = { :database => "employees", :design_doc => 'salary_finder', :view => 'find_by_salary'} options = {:endkey => "99000"} Couchdb.find view, auth_session,key=nil, options
This does a GET request to:
GET /employees/_design/salary_finder/_view/find_by_salary?endkey=99000
To query with a complex startkey and endkey, for example to query an employee database for all male employees that are 28 years old:
view = { :database => "employees", :design_doc => 'my_views', :view => 'age_gender'} options = {:startkey => ["28","male"], :endkey => ["28","male"]} Couchdb.find view, auth_session,key=nil,options
The design document for the above query will look something like this:
{ "language" : "javascript", "views" :{ "age_gender" : { "map" : "function(doc){ if(doc.age && doc.gender) emit([doc.age, doc.gender],doc); }" } } }
Just like a GET request query directly to couchDB, the options could be combined in any order.
This example below:
view = { :database => "employees", :design_doc => 'salary_finder', :view => 'find_by_salary'} options = {:startkey => "53000", :endkey => "99000",:limit => 20} Couchdb.find view, auth_session,key=nil,options
returns the first 20 employees within the salary range 53000 to 99000.
Create a design document with permanent views:
First define the views in a JSON file
//my_views.json { "language" : "javascript", "views" :{ "get_email" : { "map" : "function(doc){ if(doc.lastname && doc.email) emit(doc.id,{Firstname: doc.firstname, Lastname: doc.lastname, Email: doc.email}); }" } } }
Now create the design document and add the json file
doc = { :database => 'contacts', :design_doc => 'more_views', :json_doc => '/path/to/my_views.json' } Couchdb.create_design doc,auth_session # => {"ok"=>true, # "id"=>"_design/more_views", # "rev"=>"1-d67ae97ff03a98f68ddc300bf9ae5048"}
To query the view
view = { :database => "contacts", :design_doc => 'more_views', :view => 'get_email'} Couchdb.find view,auth_session # => [{"Firstname"=>"Nancy", "Lastname"=>"Lee", "Email"=>"nancy@mail.com"}, # {"Firstname"=>"john", "Lastname"=>"smith", "Email"=>"john@mail.com"}]
Query a View and Create it on the fly if it doesn’t already exist
Let’s say we want to query a view, and create it on the fly if it doesn’t already exist, and still return the values. In this example we will query a view called get_emails view (which returns emails of all contacts), if this view doesn’t already exist it will be added to the database.
def get_emails view = {:database => 'contacts', :design_doc => 'my_views', :view => 'get_emails', :json_doc => '/path/to/my_views.json'} Couchdb.find_on_fly(view,auth_session) end email_list = get_emails() puts email_list.inspect # => [{"Name"=>"Nancy", "Email"=>"nancy@mail.com"}, {"Name"=>"John", "Email"=>"john@mail.com"}]
In get_emails(),
Couchdb.find_on_fly(view)
sends a request to the design_document (my_views) and view (get_emails). If this view is found in the database, it returns an Array with the name and email of all contacts:
#=> [{"Name"=>"Nancy", "Email"=>"nancy@mail.com"}, {"Name"=>"John", "Email"=>"john@mail.com"}]
if the design document and view doesn’t already exist, it creates it and adds it to the database using the json document
:json_doc => '/path/to/my_views.json'
next it sends a request for the view again and then it returns the email list.
So the first time the get_emails() method is called, the view will be created on the fly and added to the database. Future calls to get_email would simply return the values from the view.
Source for the get_emails view:
//my_views.json { "language" : "javascript", "views" :{ "get_emails" : { "map" : "function(doc){ if(doc.firstname && doc.email) emit(doc.id,{Name: doc.firstname, Email: doc.email}); }" } } }
You can also query a permanent view with a key and create it on the fly, if it doesn’t already exist. Let’s say we want to view all contacts with age = “36”, and we already defined the view in a json file (view_age.json).
view = { :database => 'contacts', :design_doc => 'the_view', :view => 'age', :json_doc => '/path/to/view_age.json'} age = '36' Couchdb.find_on_fly(view,auth_session,key = age) #=> [{"_id"=>"Nancy", "_rev"=>"2-4404d0a5a1a3dff103fd46faf1e46c30", "firstname"=>"Nancy", "lastname"=>"Lee", # "phone"=>"347-808-3734", "email"=>"nancy@mail.com", "gender"=>"female", "age"=>"36"}]
The above example will query the design_document (the_view) and view (age) in the database (contacts), using the key (age). If the view doesn’t exist it will be created from the json document (view.age.json). And then return the values from the view.
This is similar to sending a
GET http://127.0.0.1:5984/contacts/_design/the_view/_view/age?key="36"
The view is generated from the source code:
//view_age.json { "language" : "javascript", "views" :{ "age" : { "map" : "function(doc){ if(doc.age) emit(doc.age,doc); }" } } }
If you are running an admin party (i.e no admins users on CouchDB), simply leave out auth_session parameter in the method.
Some examples:
view = { :database => "contacts", :design_doc => 'my_views', :view => 'get_female_contacts'} Couchdb.find view
doc = { :database => 'contacts', :design_doc => 'more_views', :json_doc => '/path/to/my_views.json' } Couchdb.create_design doc
view = {:database => 'contacts', :design_doc => 'my_views', :view => 'get_emails', :json_doc => '/path/to/my_views.json'} Couchdb.find_on_fly(view,auth_session)
This works for all methods except when you are passing a key value to the database. In that case, pass an empty string in place of the auth_session.
Example:
view = { :database => "contacts", :design_doc => 'the_view', :view => 'age'} age = "36" Couchdb.find(view,"",key = age)
And
view = { :database => 'contacts', :design_doc => 'the_view', :view => 'age', :json_doc => '/path/to/view_age.json'} age = '36' Couchdb.find_on_fly(view,"",key = age)
Query Options with Find on fly
All the query options described earlier are also work for find_on_fly(). Example:
view = { :database => 'contacts', :design_doc => 'the_view', :view => 'age', :json_doc => '/path/to/view_age.json'} age = '36' Couchdb.find_on_fly(view,auth_session,key = age, options= {:limit => 12})
The above example will return the first 12 contacts that are 36 years old.