source code bean

08 Mar, 2010

Trying out CouchDB for the first time

Posted by: Peter In: NoSQL

There are several good libraries that will abstract accessing CouchDB. However, in order to understand what goes on in the libraries, I think it is important to first understand what is going on on a lower lever, so that is what I will show you.

Step 1, install CouchDB. CouchDB for OSX can be downloaded here, the bundle contains bouth the Erlang runtime and CouchDB – no compiling or installing needed, just download the .dmg, mount it and run the application! If you are on Linux it is quite likely that CouchDB is in the repository of your distribution. In Ubuntu CouchDB is found in “Universe” and can easily be installed using apt. I haven’t tried CouchDB on Windows so I can’t give you any guidance here, if you try it out, please leave a comment.

Now when CouchDB is installed lets explore it using use one of my favorite tools, good old cURL. Port 5984 is the default port for CouchDB.

  1.  
  2. $ curl -X GET http://localhost:5984/
  3. {"couchdb":"Welcome","version":"0.10.0"}
  4.  

CouchDB is up running! Lets create a new database:

  1.  
  2. $ curl -X PUT http://localhost:5984/testdb
  3. {"ok":true}
  4.  

CouchDB’s responses are also in JSON form. Lets inspect the database we just created:

  1.  
  2. $ curl -X GET http://localhost:5984/testdb
  3. {
  4.   "db_name":"testdb",
  5.   "doc_count":0,
  6.   "doc_del_count":0,
  7.   "update_seq":0,
  8.   "purge_seq":0,
  9.   "compact_running":false,
  10.   "disk_size":79,
  11.   "instance_start_time":"1266963717052501",
  12.   "disk_format_version":4
  13. }
  14.  

CouchDB returns some statistics of the database, we can see that it contains 0 documents. Lets store an empty document in the database:

  1.  
  2. $ curl -X POST http://localhost:5984/testdb/ -H "Content-Type: application/json" -d {}
  3. {
  4.   "ok":true,
  5.   "id":"ef40feff87010a6ef3a45a16df5af977",
  6.   "rev":"1-967a00dff5e02add41819138abb3284d"
  7. }
  8.  

CouchDB returns the unique id for the document and the version number (did i mention that all documents are version controlled?:)). Next step is to fetch all documents:

  1.  
  2. $ curl -X GET http://localhost:5984/testdb/_all_docs
  3. {
  4.   "total_rows":1,
  5.   "offset":0,
  6.   "rows":[{
  7.     "id":"ef40feff87010a6ef3a45a16df5af977",
  8.     "key":"ef40feff87010a6ef3a45a16df5af977",
  9.     "value":{"rev":"1-967a00dff5e02add41819138abb3284d"}
  10.   }]
  11. }
  12.  

Yes! it is there! Now you should also be able to see your database “testdb” and the document you created in the CouchDB GUI.

No Responses to "Trying out CouchDB for the first time"

Comment Form

Categories

Adwords

Twitter Updates


    • Petan: I got in bootstrap this (insted of $frontController->getRouter()): $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'rout
    • oanh tong ngoc: :) It's usefull but could U give everyone's an example with a project source code. Thanks
    • Peter: Hi Sohaib, It seems like the rewrite module isn't loaded by IIS. Have you uploaded the UrlRewriter dlls and made the changes to web.config on the ser

    About

    Welcome to source code bean! You will find information on tips and tricks on programming languages, server side stuff, and anything that causes troubles to web development.