You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Avi Flax <av...@arc90.com> on 2008/07/20 18:44:42 UTC

Newbie: How to create a View

I must be missing something, but after scouring the website and searching
the list via markmail, I can't seem to find a straight-forward explanation
of how to create a View. Could someone please point me to one?
Thanks,
Avi

-- 
Avi Flax » Lead Technologist » Partner » Arc90 » http://arc90.com

Re: Newbie: How to create a View

Posted by Avi Flax <av...@arc90.com>.
On Tue, Jul 22, 2008 at 12:34 PM, Jan Lehnardt <ja...@apache.org> wrote:

> Coming in late here, but I assume you have seen
> http://wiki.apache.org/couchdb/HttpViewApi

Thanks Jan, yes, I had, but at the time it wasn't clicking. Now it
seems obvious. I think this illustrates the value of step-by-step
howtos.

Avi

Re: Newbie: How to create a View

Posted by Jan Lehnardt <ja...@apache.org>.
On Jul 20, 2008, at 17:44 , Avi Flax wrote:

> I must be missing something, but after scouring the website and  
> searching
> the list via markmail, I can't seem to find a straight-forward  
> explanation
> of how to create a View. Could someone please point me to one?

Coming in late here, but I assume you have seen
http://wiki.apache.org/couchdb/HttpViewApi

Cheers
Jan
--

Re: Newbie: How to create a View

Posted by Sho Fukamachi <sh...@gmail.com>.
On 21/07/2008, at 5:27 AM, Avi Flax wrote:


> While I can now create and "call" a view, I'm still a little confused
> as to the terminology involved and the intended usage patterns.

Yes, it's a very foreign way of thinking if you're coming from SQL- 
land. Expect a few hours of staring blankly at strange-looking  
javascript and thinking "Huh!?". It'll click eventually though : )


> For instance, you used the term "namespace". What might a namespace
> be? Also, the design document includes the key "views" - are there
> other keys supported? Or is that TBD? Also, apparently I can define
> multiple view functions in the "view" key - that's cool, but I don't
> quite understand the intended usage pattern.
>
> For example, how should I name my namespaces and my views?

Well, I think this is completely up to you. You should probably name  
them loosely under their "category". If you look at how some of the  
libraries do it, they name them after the document type they're  
intended to be associated with - for example if you have the doc.type  
"user" you might put any views intended to work on them _view/users.

The "view" key is as far as I know hardcoded and permanent, you're  
free to create as many design documents with whatever _design/names  
you want but that key is necessary.


> Perhaps like this?
>
> http://localhost:5984/vehicles/_view/cars/get_all_cars
> http://localhost:5984/vehicles/_view/cars/get_car
>
> http://localhost:5984/vehicles/_view/trucks/get_all_trucks
> http://localhost:5984/vehicles/_view/trucks/get_truck

Well they seem pretty reasonable to me. However, you could save  
yourself some duplication by defining more than one key in the view  
and having something like this:

http://localhost:5984/vehicles/_view/shared/get_all_of_type
http://localhost:5984/vehicles/_view/shared/get_one_with_type

and then pass the "type" string into it. Obviously that depends how  
you are storing the type etc.


> ...? Or maybe I'm missing the point, perhaps it's intended to be as
> open-ended as it is, so we can make up our own usage patterns.

I think you've hit the nail on the head there!

Sho

Re: Newbie: How to create a View

Posted by Avi Flax <av...@arc90.com>.
On Sun, Jul 20, 2008 at 1:34 PM, Sho Fukamachi <sh...@gmail.com> wrote:

> as a fellow newbie embarked on a CouchDB crash course I hope I can help.

Sho, thanks so much! This is perfect, exactly what I needed. It's a
bit exasperating that this sort of simple how-to info isn't to be
found on the website.

> Can you access Futon, the couchdb web interface?

Yup, I'm using CouchDBX (*very* useful btw).

> Let me know how you go. I agree the docs could be a little easier to understand, maybe I will try to write up something "from zero"...

That would be really, really helpful.

While I can now create and "call" a view, I'm still a little confused
as to the terminology involved and the intended usage patterns.

For instance, you used the term "namespace". What might a namespace
be? Also, the design document includes the key "views" - are there
other keys supported? Or is that TBD? Also, apparently I can define
multiple view functions in the "view" key - that's cool, but I don't
quite understand the intended usage pattern.

For example, how should I name my namespaces and my views?

Perhaps like this?

http://localhost:5984/vehicles/_view/cars/get_all_cars
http://localhost:5984/vehicles/_view/cars/get_car

http://localhost:5984/vehicles/_view/trucks/get_all_trucks
http://localhost:5984/vehicles/_view/trucks/get_truck

...or...

http://localhost:5984/vehicles/_view/data/this
http://localhost:5984/vehicles/_view/data/that

http://localhost:5984/vehicles/_view/reporting/this
http://localhost:5984/vehicles/_view/reporting/that

...? Or maybe I'm missing the point, perhaps it's intended to be as
open-ended as it is, so we can make up our own usage patterns.

Thanks!!!
Avi

Re: Newbie: How to create a View

Posted by Avi Flax <av...@arc90.com>.
On Mon, Jul 21, 2008 at 5:32 AM, Christopher Lenz <cm...@gmx.de> wrote:

> Actually, it's a bit simpler than that :)
>
> 1. If you don't already have a database, create one.
> 2. Switch the "Custom Query" in the view dropdown at the top right.
> 3. Type in your query and test/refine it until it does what you want it to
> do.
> 4. Click the "Save As" button at the bottom right.
> 5. In the dialog, enter the design doc ID and the name of the view.
> 6. You're done.

Excellent!

Thanks Christopher!

Avi

Re: Newbie: How to create a View

Posted by Christopher Lenz <cm...@gmx.de>.
On 20.07.2008, at 19:34, Sho Fukamachi wrote:
> as a fellow newbie embarked on a CouchDB crash course I hope I can  
> help.
>
> Can you access Futon, the couchdb web interface? If you can, create  
> a database $MY_DB and switch to it.
>
> Choose a namespace for your view ($VIEW) (ie just make something  
> up!). Then create a new document in that database with the name  
> "_design/$VIEW".
>
> Futon should now switch to your new document. Create a field in it  
> called "views". Save.
>
> Futon is a bit weird about how it presents text editors for fields,  
> so first put "{}" into the views field and save. Then, double click  
> it again and you should have a nice big text window.
>
> Now copy this into the views field:
>
> {
>   "get_all": {
>       "map": "function(doc) { emit(null, doc); }"
>   }
> }
>
> And save.

Actually, it's a bit simpler than that :)

1. If you don't already have a database, create one.
2. Switch the "Custom Query" in the view dropdown at the top right.
3. Type in your query and test/refine it until it does what you want  
it to do.
4. Click the "Save As" button at the bottom right.
5. In the dialog, enter the design doc ID and the name of the view.
6. You're done.

Cheers,
--
Christopher Lenz
   cmlenz at gmx.de
   http://www.cmlenz.net/


Re: Newbie: How to create a View

Posted by Sho Fukamachi <sh...@gmail.com>.
Avi,

as a fellow newbie embarked on a CouchDB crash course I hope I can help.

Can you access Futon, the couchdb web interface? If you can, create a  
database $MY_DB and switch to it.

Choose a namespace for your view ($VIEW) (ie just make something up!).  
Then create a new document in that database with the name "_design/ 
$VIEW".

Futon should now switch to your new document. Create a field in it  
called "views". Save.

Futon is a bit weird about how it presents text editors for fields, so  
first put "{}" into the views field and save. Then, double click it  
again and you should have a nice big text window.

Now copy this into the views field:

{
    "get_all": {
        "map": "function(doc) { emit(null, doc); }"
    }
}

And save.

Now hit your database at

http://localhost:5984/$MY_DB/_view/$VIEW/get_all

and you should see your view in action! Change/add more as you see fit.

Let me know how you go. I agree the docs could be a little easier to  
understand, maybe I will try to write up something "from zero"...

Sho



On 21/07/2008, at 2:44 AM, Avi Flax wrote:

> I must be missing something, but after scouring the website and  
> searching
> the list via markmail, I can't seem to find a straight-forward  
> explanation
> of how to create a View. Could someone please point me to one?
> Thanks,
> Avi
>
> -- 
> Avi Flax » Lead Technologist » Partner » Arc90 » http://arc90.com