You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Michael W <li...@michaelgruetzner.com> on 2010/02/26 16:11:51 UTC
Understanding views
Hi everybody,
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.
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?
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?
Thank you very much in advance for answering my questions.
Have a nice weekend!
Michael
RE: Understanding views
Posted by Nils Breunese <N....@vpro.nl>.
1) A view has a name which needs to be unique in a design document. Other than that there is no problem storing multiple views in a single design document. I recommend managing you design documents using CouchApp (also makes it easy to manage your design documents in a version control system): http://groups.google.com/group/couchapp
2) See the documentation for the HTTP View API for what parameters you can pass to views: http://wiki.apache.org/couchdb/HTTP_view_API You might also want to check out list and show functions, which add extra flexibility on top of views: http://wiki.apache.org/couchdb/Formatting_with_Show_and_List
Nils.
________________________________________
Van: Michael W [lists@michaelgruetzner.com]
Verzonden: vrijdag 26 februari 2010 16:11
Aan: user@couchdb.apache.org
Onderwerp: Understanding views
Hi everybody,
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.
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?
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?
Thank you very much in advance for answering my questions.
Have a nice weekend!
Michael
De informatie vervat in deze e-mail en meegezonden bijlagen is uitsluitend bedoeld voor gebruik door de geadresseerde en kan vertrouwelijke informatie bevatten. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking van deze informatie aan derden is voorbehouden aan geadresseerde. De VPRO staat niet in voor de juiste en volledige overbrenging van de inhoud van een verzonden e-mail, noch voor tijdige ontvangst daarvan.
Re: Understanding views
Posted by Jon Gretar Borgthorsson <jo...@gmail.com>.
On Fri, Feb 26, 2010 at 3:11 PM, Michael W <li...@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"