Sunday, September 20, 2015

Couch DB

What is CouchDB ?
  • It's an open source NO-SQL database as we don't use the legacy SQL Statements to query the DB.
  • It's a document storage Database as it holds the collection of independent documents [Document holds the data as the JSON Objects.].
  • Uses HTTP based API  for CRUD Operations.
Why CouchDB?
  • Have HTTP Based API to retrive the Data.
  • Follows Atomicity while saving the Data into Documents. Either all Data is saved or not saved at all.
  • Have Views that would allow to join many Documents to have multiple view of Data.
Setting up CouchDB
  • We can either have our own DB at Cloud using
  • Else Avail the local CouchDB from Once Setup, access the Futon(admin console) at if it's been locally setup.
  • CouchDB allows you to write a client side application that talks directly to the Couch without the need for a server side middle layer, significantly reducing development time.
  • cURL is a tool available in Linux, Mac, Windows etc Platforms and can be used to transfer the data to a CouchDB via HTTP, Telnet, HTTPS, FTP and LDAP Protocols.  
  • Below command returns the UI part of accessed URL.


Flags in Curl

-X Flag = tells curl to use the provided request method and not the default i.e. GET
-d Flag = helps in passing the data along with  Request methods.

curl -X PUT -d userid=ttt -d password=ttt

-o Flag writes the requested url into a file

curl -o index.html

Interactions with CouchDB via cURL
  • Check Connections with CouchDB [ curl ]
  • List all DBs of CouchDB [curl -X GET ]
  • Create a new DB in CouchDB [curl -X PUT ]
  • Verify if the new DB been created [curl -X GET]
  • Get the Information about the newly Created DB [curl -X GET ]
  • Create a new Document in CouchDB with Document ID[ curl -X PUT"testerid" -d '{"lockerno": "123411","facility":"inot"}' ]
  • Return values of a Document with Document ID. [ curl -X GET"testerid" ]
  • Delete a Document with Document Id and Revision Id [ curl -X DELETE ]

Maps & Reduce

Open CouchDB -> Select any DB -> Navigate to Views Drop Down on right and Select Temporary Views.

Sample Maps

function (doc) {
    if ( === "Ananda") {

Sample Reduce

function (keys,value) {
    return sum(value);

[ Note: This would iterate through all the Documents in that DB and return the filtered result. The filtered result then get reduced by reduce function. ]

Map and Reduce Functions offer great variations to the Document to the document Structure and the resultant is called View in CouchDB Terminology.

Default Sort Order is based on the IDs to makes the Views efficient.

Value from the Map Function gets passed onto the second attribute [normally marked as value as above] of Reduce Function.

Group Level Re-reduce:

In certain map function, you may have group of keys as Array as below:

function(doc) {
var store, price, value;
if (doc.item && doc.price)
for (store in doc.price) {

Array of Keys in Map Emit Function can be helpful in performing group level re-reduce emit([,doc._id],doc.value);

Let's then have the sample reduce function as below:

function(keys, values) {
    return values.length;

Now if we enable reduce check button, we can find the value filtered producing the key as [doc._id,store]. The default group level reduce is exact. In case of group level 1 reduce, we have only one key [doc._id] available. When on group level 2 reduce, we have all two keys available.

B Tree Index:

CouchDB employs B Tree indexing to store and retrive the Values internally. You can have a look at Wiki Page for more info on B Tree []

No comments:

Post a Comment