Document-Oriented Persistence with CouchDB

… a scalable, NoSql database

CouchDB Design Documents

with one comment

Hello folks, I’m back. Did you miss me? Oh how nice of you to say so.

So when we last chatted I mentioned I was doing my Master’s project about CouchDB.

To get started with CouchDB, go and get a CouchDB instance hosted by the wonderful people at

Today, I want to talk a little about design documents in CouchDB.

Don’t be scared, stay with me and we’ll have a great time.

A design document in CouchDB is a document which stores application code.

Let’s see if we can try to understand this design document, and what it is doing.

Look at this url – it’s a little bit crazy.

So, what does this crazy url consist of? is a database.

To see it in “glorious technicolor”, you can use the built-in CouchDB utility, Futon:

Are you more of a purist? Want to see all the documents in this database in no-frills json format?
Note that what you see here is a set of key-value pairs.

You put data in to CouchDB in the form of JSON documents.
Internally, CouchDB stores the data in the form of indexed key-value pairs.

Anyway, sorry for that digression, let’s return to that crazy url. is our database.

CouchDB is a document-oriented database – it contains – yes, you guessed it – documents.

Most of the documents contain data, in our case data to do with universities, like this one.

One of the many amazing things about CouchDB is that documents can contain anything.

So, as well as having documents that contain data, we can also – wait for it – have documents that contain code as well.

As we all know it’s a very very good idea to separate code from data.
So, CouchDB likes us to put code in design documents.

The way I like to think about it is that a design document is like an application, using the data in the database.

Anyway, because I am very unimaginative I have called my application “default”.

So, to create my application, I create a design document, and give it an id according to CouchDB’s special naming convention.

The database is “universities”, the name of the document is “_design/default” – it’s a little confusing to have a forward slash in the name of the document, but that’s the convention that CouchDB uses for design documents.

Within the design document, there is a section named “shows”. This gives us a way to show the data for a particular document.

"id":"function(doc,req){ return '<h1>' + doc._id + '</h1>' }"

Here, I’ve defined one show, called “id”. It’s a function which accepts as parameters a document (doc) and a request object (req).

This is the url to access this show.

This one is very simple. You can see how you could use this to build up a web page showing all the data relating to a particular university.

That’s what I intend to do next.


Written by klena02

August 10, 2010 at 9:51 pm

Posted in CouchDB

One Response

Subscribe to comments with RSS.

  1. […] Last time we looked at how design documents can hold HTML, and apply the HTML to any document we have. […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: