You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Andreas <an...@markup.nu> on 2009/03/03 00:52:12 UTC

View related question (getting /database/slug in addition to /database/id)

Hi,

I am just starting to play with CouchDB and I read a lot of blog posts, 
apache's wiki pages and the published pages of the upcoming book. So far 
it feels weird (always worked with relational databases), but I am 
learning fast I think.

I have a question I could't find an answer to, I hope it doesn't sound 
too dumb ;-)

Let's say I have this database, I have an id, a title, a slug and a 
body. If I GET /database/id it works just fine, however I'd like to be 
able to GET /database/slug as well. How do I go for it?

I realize I could set an arbitrary "id", so that I could GET 
/database/sometext and pretend it was the slug while I am actually 
calling an id, but I'd like to be able to do both.

I also know I could create a view like this:

function(doc) {
   if(doc.slug) {
     emit(doc.slug, null);
   }
}

to get all the items with a given slug (GET 
/database/_view/posts/by_slug/?key="sometext"), but the slug is going to 
be a unique string, so it seems a superflous method. By the way: why do 
I have to pass quotes to the key value? Am I doing something wrong with 
this view?

So: is there any way for me to GET /database/slug in addition to GET 
/database/id ?

Thanks,

Andreas

Re: View related question (getting /database/slug in addition to /database/id)

Posted by Chris Anderson <jc...@apache.org>.
On Mon, Mar 2, 2009 at 3:52 PM, Andreas <an...@markup.nu> wrote:
> Hi,
>
> I am just starting to play with CouchDB and I read a lot of blog posts,
> apache's wiki pages and the published pages of the upcoming book. So far it
> feels weird (always worked with relational databases), but I am learning
> fast I think.
>
> I have a question I could't find an answer to, I hope it doesn't sound too
> dumb ;-)
>
> Let's say I have this database, I have an id, a title, a slug and a body. If
> I GET /database/id it works just fine, however I'd like to be able to GET
> /database/slug as well. How do I go for it?
>
> I realize I could set an arbitrary "id", so that I could GET
> /database/sometext and pretend it was the slug while I am actually calling
> an id, but I'd like to be able to do both.
>

That's the way to do it. Set the id = the slug.

> I also know I could create a view like this:
>
> function(doc) {
>  if(doc.slug) {
>    emit(doc.slug, null);
>  }
> }
>
> to get all the items with a given slug (GET
> /database/_view/posts/by_slug/?key="sometext"), but the slug is going to be
> a unique string, so it seems a superflous method. By the way: why do I have
> to pass quotes to the key value? Am I doing something wrong with this view?
>

nope. that's just right. you seem to understand it exactly.

The reason the key is quoted is because it must be a json value. so it
can be startkey=[1,2,"foo",{"a":null}] if you need it to be.

Chris


-- 
Chris Anderson
http://jchris.mfdz.com