Tag Archives: views

CouchDB, Design Documents

In this post i will try to talk about another concept of CouchDb “Design Documents” ( you can read the last post that i wrote about CouchDB views it will be a reference for the rest of this content)

So what are Design Documents ?? in the last tutorial we created temporary views then we stored them into permanent views. These views are stored on a special document called Design Document ! Let’s see what we have on futon :

You have to go to : contact database  > then choose View “Design Documents” :

Then choose “_design/contacts” :

You will notice that also a design Document is a Document ! Let’s explain what we have here :

language : is the language used for the views, usually it’s Javascript but in the latest Versions of CouchDB you can use Erlang.

views : contains all the views included with the contact document, and for every view you can find the map and reduce functions :

   "_id": "_design/contacts",
   "_rev": "1-a8487fd2a8b3698a19a5ae6dd59b178d",
   "language": "javascript",
   "views": {
       "phones": {
           "map": "function(doc) {
                  temit(doc._id, doc.phone);

Creating permanent Views using Curl

We already know how to execute a view using curl :

curl -X GET

Now we will try to create a new view to get “contact names”, so first of all we need to create our new Design Document :

  "language" : "javascript",
  "views" : {
    "contact_names" : {
      "map" : "function(doc){
         emit(doc._id, {Name : doc.firstName});

As you can see the json document have the same structure of the first Design Document.
Now let’s tell Couch to add our new json definition :

curl -X PUT -d @new_design.json

will return :


Now to call the new view :

curl -X GET

getting :


That’s all ! hope you enjoy using CouchDB :)

CouchDb, Creating views

The first couchDb tutorial that i’ve wrote was about simple operations like creating, updating or deleting documents, in this post i will explain how to create views, and how to call them ;

Creating views

The simplest way to create views is to use “Futon” (the phpmyadmin like for couchDb) accessible via :


And if everything is ok we should have something like this :

Then choose database where to create View, then on the select box in front of ‘View‘ select ‘Temporary view‘ :

When you click run button :

the last time we have created database called ‘contacts‘ with documents having this structure :

   "_id": "johnsmith",
   "_rev": "3-94052866ba060f57508fde265a243b47",
   "firstName": "John",
   "lastName": "Smith",
   "email": [
   "phone": "(555) 555-5555"

The default function to show database content is :

function(doc) {
  emit(null, doc);

And when you hit run it will display all entries from the database “contact“, know let’s try to get contacts with a specific criteria, for example : “all contacts having a phone number” :

function(doc) {
	emit(doc._id, doc.phone);

As result :

Saving the temporary view

Okey now that we are satisfied of the result let’s save the temporary view a permanent one :

  1. Click “Save As” button
  2. Set a design Name
  3. Set a view Name

That’s all ! our view will be accessible from the “View” Select Box :

Getting view result with curl

To query the server will use curl :

curl -X GET

Result :

  {"id":"johnsmith","key":"johnsmith","value":"(555) 555-5555"}

Some explanations about params passed to curl :
– First argument “contact” is database name
– Second argument “_design” with value “contacts” is the design name that we have set when we saved the view
– Third argument “_view” with value “phone” is simply the view name

We can also pass other params at the end to tell the server how many entries to return, or to order them :

curl -X GET

Hope this was helpful to understand how to create simple views with couchDb.