• A scalable, fault-tolerant, and schema-free
document-oriented database
• A RESTful HTTP/JSON API accessible from
many programming libraries and tools
• Incremental Map/Reduce queries written in
any language (JavaScript support built-in)
• Incremental and flexible replication with
conflict management
What does it means?
• It means that you don’t need to think to much
upfront about what your data will be
structured like. You don’t need to think about
relations or what might be needed for the
future. You just put the data as it currently is
into the database and in case it changes, you
just change the documents.
Relational model
id firstName lastName
1 Danila Prepeleac
2 Gheorghe Doja
customerId phoneTypeId phoneNumber
1 1 004075123321
1 2 064500100
2 1 +4072600300
Customers
Phones
id Type
1 Mobile
2 Home
PhoneTypes
CRUD Operations
• CouchDB speaks the language of the web:
REST, HTTP, and JSON are how CouchDB works
natively
• Calls are made to the database via HTTP (we
can use anything that talks HTTP)
• Responses come back as JSON
Create database:
bash$ curl -X PUT http://localhost:5984/contacts
{"ok":true}
bash$ curl -X PUT http://localhost:5984/contacts/1 -d
'{"firstName":"Gheorghe","lastName":"Doja","phones":{"mobile":"004075123321","home":"4500100"}}'
{"ok":true,"id":"1","rev":"1-c7762a779effe1398d8d2ea36ff958a6"}
bash$ curl -X GET http://localhost:5984/contacts/_all_docs
{"total_rows":1,"offset":0,"rows":[
{"id":"1","key":"1","value":{"rev":"1-c7762a779effe1398d8d2ea36ff958a6"}}
]}
Get all documents from a database
bash$ curl -X GET http://localhost:5984/contacts/1
{"_id":"1","_rev":"1-
c7762a779effe1398d8d2ea36ff958a6","firstName":"Gheorghe","lastName":"Doja","phones":{"mobile":"004075123321","home":"064500100
"}}
Get a specific document
bash$ curl -X PUT http://localhost:5984/contacts/1 -d '{"_id":"1","_rev":"1-c7762a779effe1398d8d2ea36ff958a6","firstName":"Gheorgh
e","lastName":"Doja","phones":{"mobile":"004075123000","home":"+4064500100"}}‘
{"ok":true,"id":"1","rev":"2-875f4f380fe5e3eeb5fbaeb495fc3599"}
Updates a document
bash$ curl -X DELETE http://localhost:5984/contacts/1?rev="2-875f4f380fe5e3eeb5fbaeb495fc3599"
{"ok":true,"id":"1","rev":"3-de88253ac3cb9463aac609d5fdfd3135"}
Delete a document
Create a new document:
Updating documents
• Highly concurent
• It can serve a high number of parallel
requests.
• MVCC (Multiversion concurrency control)
Views
• Are stored as an accessible web resource on
disk and incrementally updated as well as
replicated with the database
• Each view defines a map function, and can
define a reduce function
One simple view
function(doc) {
if (doc.ocupation == "domnitor") {
emit(doc.ocupation, doc.firstName + ' ' + doc.lastName);
}
}
input
output
key value