couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Anderson <>
Subject Re: View related question (getting /database/slug in addition to /database/id)
Date Mon, 02 Mar 2009 23:57:07 GMT
On Mon, Mar 2, 2009 at 3:52 PM, Andreas <> 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 Anderson

View raw message