CouchDB: A brief introduction (Part II)

Tagged:

Views, What are? and how to use them?

A view is nothing else than a regular CouchDB document that has an associative array call views that holds all the views. Each view is also represented by an array that hold two functions, map and reduce. Futhermore, this document must have and identifier following this way "_design/" followed by the name of our design document.

To get the view results we must use the next URL in a navigator:

http://url_of_the_server:5984/database/_design/document_id/_view/name_of_the_view

The map function:

The map function is called once by every document in the database, using that document as input parameter of the map function. This means that map function only knows the values of the document that have been used as parameter. Even if this seems a limitation, doing the things in this way CouchDB could generate the results in an incremental way and can do it in a propper form to work in parallel.

The result of the map function is to generate rows  with one key-value pair for each row. Each document can generate as many rows as you want. For example, If we want to get the name and the telephone number of every presentation card in our database, we should build a document with "_design/phones" id and only will have the next map function:

 

function (doc) {

  for (phone in doc.telephone) {

      emit(doc.name, doc.telephone[phone]);

  }

}

In this way we will get a list of key-value pairs, which have a key that will be the name of the person and the value will be the telephone of that person. If someone has more than one telephone number, we will get as many rows for that person as telephones numbers are in the database saved.

 

The reduce function:

This function works with the output of the map function, and if its needed will use their own output. This function is used to group values, make some calculation, and any other proccess that have dependencies with any other document. It has three parameters, the first one is an array of keys, the second one an array of values, both from the output of map function. The third one is an array with the output of the last reduce call.

 

Taking the presentation card example, if we wan't to group all the phones by name we must make a reduce function like this one:

 

function(keys, values, combine) {

        return value;

     }

 

This function will give key-value pairs which will have as key the name of all the people in the database and as value an array with all the telephone numbers.

 

The final document, putting together both functions, will be like this:

 

{
"_id": "_design/phones",
"_rev": "1-1520601632",
"language": "javascript",
"views": {
"byname": {
"map": "function(doc) {
for(phone in doc.telephone) {
emit(doc.name, doc.telephone[phone]);
}
}"
,
"reduce": "function(keys, value, combine){
return value;
}"

}
}
}

Next...

In the next post I'll explain how View Collation works and how to filter the output of a view using parameters in the URL. If you have any comment, doubt or sugestion fell free of leave a comment.