couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jon Gretar Borgthorsson <jon.borgthors...@gmail.com>
Subject Re: Understanding views
Date Fri, 26 Feb 2010 15:59:08 GMT
On Fri, Feb 26, 2010 at 3:11 PM, Michael W <lists@michaelgruetzner.com>wrote:

> I just started to get familiar with couchDB. One of the most important
> resource of my research is the draft of O'Reily's book on couchDB. I belive
> I've got a pretty good overview on how couchDB works but there still some
> questions regarding views which I hope you can help me to answer.
>

I highly recommend reading CouchDB: The Definitive Guide (
http://books.couchdb.org/relax/)
It's a good start.


>
> 1) According to the documentation (if I understand correctly) views can be
> stored only in design documents and running ad-hoc views is slower than
> running pre-defined views (from the ddoc). How can I create multiple views
> in one ddoc?
> A real-life app wil probably need dozens views and I guess it's not the
> recommended way to store each of them in a single ddoc, is it?
>

Often we group the views into a few ddocs. There is nothing that stops you
having all in one ddoc and nothing thats stops you having many ddocs. Many
people have one ddoc for each application that uses the database. Just use
what feels natural to you. Like having one ddoc for views related to users
and another for views relating to articles.

A  ddoc with many views would look something like this. (by memory at least)

{
"_id":"_design/users",
"views": {
  "by_name":{map:"FUNCTION", reduce:"FUNCTION"},
  "by_id":{map:"FUNCTION"}
}



>
> 2) How can I pass additional parameters to a view? Consider an application
> where the user needs to log in and the user id is stored somewhere in the
> session object. Now I want to query couchDB to get all documents of a
> specific type which the current user has created. How could I pass that
> user
> id to the view function?


By creating a view listing the parameters you wish to view for in the key.
Then using key for finding a single record or startkey, endkey for finding a
list of records.

Like this example from http://books.couchdb.org/relax/design-documents/views

/blog/_design/docs/_view/by_date?key="2009/01/30 18:04:11"
/blog/_design/docs/_view/by_date?startkey="2009/01/01
00:00:00"&endkey="2009/02/00 00:00:00"

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message