CouchDB For Weekend Coders
Oct. 25th, 2010 01:10 am![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Today I've been trying to get my head around CouchDB. And yes, when you finally understand it, it is alarmingly simple.
CouchDB is one of those newfangled NoSQL databases. Specifically, it's a document store. This initially left me scratching my head, since it has no tables and no schemas. Records in CouchDB are a collection of arbitrary key value pairs.
The key to understanding how to make use of this all would appear to be views. Views are analogous to indices in an RDBMS. They are composed of two JavaScript functions—map and reduce. This is already getting into scary functional programming territory and you can see me glancing around for the exit at this point.
It's actually not that bad. Map takes a document as its parameter and emits a key value pair. This pair is then inserted as a leaf in a search tree. (Specifically a B-Tree). Every time you create or update a document it is passed to the various view functions to update these views.
The important thing here (the bit that I took my time getting my head around) is that the key in this pair need not be unique within the view. You can also then query these views by key. This gives you your entity relationships.
The reduce part of the view gives you the aggregate functionality of an RDBMS. The number of employees grouped by city, to use the archetypical example. Reduce takes a collection of values and returns a single value. These values are then stored in the branches of the search tree to accelerate future aggregate queries on slices of the keyspace.
There are also validation and transformational functions, but those are a bit more straight forward. What I hope I've noted down here is the answer to the question I was asking myself this morning, "How are you supposed to use a NoSQL database?"
CouchDB is one of those newfangled NoSQL databases. Specifically, it's a document store. This initially left me scratching my head, since it has no tables and no schemas. Records in CouchDB are a collection of arbitrary key value pairs.
The key to understanding how to make use of this all would appear to be views. Views are analogous to indices in an RDBMS. They are composed of two JavaScript functions—map and reduce. This is already getting into scary functional programming territory and you can see me glancing around for the exit at this point.
It's actually not that bad. Map takes a document as its parameter and emits a key value pair. This pair is then inserted as a leaf in a search tree. (Specifically a B-Tree). Every time you create or update a document it is passed to the various view functions to update these views.
The important thing here (the bit that I took my time getting my head around) is that the key in this pair need not be unique within the view. You can also then query these views by key. This gives you your entity relationships.
The reduce part of the view gives you the aggregate functionality of an RDBMS. The number of employees grouped by city, to use the archetypical example. Reduce takes a collection of values and returns a single value. These values are then stored in the branches of the search tree to accelerate future aggregate queries on slices of the keyspace.
There are also validation and transformational functions, but those are a bit more straight forward. What I hope I've noted down here is the answer to the question I was asking myself this morning, "How are you supposed to use a NoSQL database?"