RK !

Let's make comprehension easy ...

100%x200

Couch DB

Author: Romaan, Last Updated: Jan. 5, 2021, 6:45 a.m.

Couch DB is a database management system that works on the concept of document key store. All it means is that Couch DB can store a document and has a key to uniquely identify the document. Unlike traditional relational database management system, we won't be able to use SQL to manage data or define schemas. The only way to interact with CouchDB is via HTTP requests. It support all actions namely GET, PUT, DELETE and POST.

CouchDB is a document key store.
 

Quick Things to try:

  • First message to try:
$ curl http://localhost:5984{"couchdb":"Welcome","uuid":"947fc85cd3d64ef9421466f4f3ef49e0","version":"1.5.0","vendor":{"version":"14.04","name":"Ubuntu"}}
  • Get list of all databases
$ curl -X GET http://127.0.0.1:5984/_all_dbs["_replicator","_users","identity","pair_event","pair_state","role","role_identity","seats","security","support_log","support_state"]
  • Create database
$ curl -X PUT http://127.0.0.1:5984/<new_db_name>
  • Delete database
$ curl -X DELETE http://127.0.0.1:5984/<new_dn_name>
  • Get a document
$ curl -X GET http://127.0.0.1:5984/<db_name>/<key>
  • Store a document -> TODO: What about id? 
$ curl -X POST http://127.0.0.1:5984/<db_name> -H 'Content-Type: application/json' -d '{"id":"0856aa4839e3bc63733a1e980400222a","uuid":"cba3eb8e-1498-11e5-a9eb-005056b705f9","last_signin_ip":"126.112.245.98","realm_uuid":"97b05d0e-4611-11e5-87d3-9890969f4100","last_signin_time":"2015-08-16 23:41:42","external_ids":[{"email":"test@dev-mail.com"}],"type":"identity"}'
  • Creating Design Document
$ curl -X POST http://127.0.0.1:5984/<db_name> -H 'Content-Type: application/json' -d '{
  "_id" : "_design/example",
  "views" : {
    "foo" : {
      "map" : "function(doc){ if (doc.type == "<type_name>") emit(doc.field, doc) }"
    },
    "bar": {
      "map" : "function(doc){ if (doc.type == "<type_name>") emit(doc.other_field, doc) }"
    }
  }
}'
  • Calling the design we created, and querying the data
$ curl http://127.0.0.1:5984/<db>/_design/example/_view/foo
$ curl http://127.0.0.1:5984/<db>/_design/example/_view/foo?key='xyz'
$ curl http://127.0.0.1:5984/<db>/_design/example/_view/foo?startkey='abc'&endkey='xyz'
  • Map Reduce Document
{
   "by_org_and_type": {
       "map": "(doc) ->\n  for type, count of doc.subscriptions\n    emit([doc.org_id, type], count)\n",
       "reduce": "(keys, values, rereduce) ->\n  sum(values)\n"
   },
   "last_active": {
       "map": "(doc) ->\n  for type, count of doc.subscriptions\n    emit([doc.org_id, type, doc.last_active], null)\n"
   }
}
 

Importing and Exporting data from/to file

  • Inorder to dump into file from couchdb:
curl -X GET http://localhost:5984/DATABASE_NAME/_all_docs?include_docs=true > FILENAME.txt
  • Inorder to import file into couchdb
curl -d @FILENAME.txt -X POST http://localhost:5984/DATABASE_NAME/_bulk_docs

However if you get a problem during importing with an error: "Missing JSON list of docs", please edit the FILENAME.txt and change

{"total_rows": 8244, "offset": 0, "rows": [

to

{"docs": [

 

 

 

Popular Tags:


Related Articles:


Comments: