Catalog / CouchDB Cheatsheet
CouchDB Cheatsheet
A comprehensive guide to Apache CouchDB, covering basic concepts, querying, administration, and common tasks.
Core Concepts
Basic Definitions
Document |
A JSON document that is the basic unit of data in CouchDB. It has a unique |
Database |
A collection of documents. Each CouchDB instance can host multiple databases. |
View |
A function (written in JavaScript or another language) that transforms documents into a queryable index. Uses MapReduce. |
MapReduce |
A programming model for processing large datasets with a map function that transforms data and a reduce function that aggregates the mapped results. |
Replication |
The process of synchronizing databases between CouchDB instances, allowing for distributed data storage and offline access. |
Conflicts |
Occur when the same document is updated concurrently on different nodes during replication. CouchDB resolves conflicts by choosing a winning revision and storing the conflicting revisions as a history. |
Document Structure
A CouchDB document is a JSON object with special fields:
Example:
|
Key Concepts Illustrated
Imagine a database of books.
|
Querying with Views
Creating Views
Views are defined within design documents. A design document is a special document whose Example:
This creates a view named |
Querying Views
To query a view, use the following URL:
Example:
This will return all books by John Doe. Common Query Parameters:
|
MapReduce Functions
Map Function |
Processes each document and emits key-value pairs. The |
Reduce Function |
Aggregates the results of the map function. It takes keys, values, and a |
Example Map Function |
|
Example Reduce Function |
|
CouchDB API
Database Operations
Create Database |
|
Get Database Info |
|
Delete Database |
|
List All Databases |
|
Document Operations
Create Document |
|
Get Document |
|
Update Document |
|
Delete Document |
|
Bulk Operations
Bulk operations allow you to perform multiple document operations in a single request, improving performance.
|
Administration and Maintenance
Configuration
CouchDB is configured through a configuration file ( Key Configuration Sections:
Example Setting:
|
Replication
CouchDB supports continuous and one-time replication. Replication can be configured using the Example Configuration Document in
|
Compaction
Compaction removes unused data and optimizes database storage. It is triggered automatically, but can also be initiated manually. To compact a database:
To compact a design document (and its views):
|
Security
Authentication |
CouchDB supports various authentication methods, including Basic Authentication and Cookie Authentication. |
Authorization |
Access control is managed through roles. Users can be assigned roles to grant them specific permissions. |
Admin Party |
A mode where all requests are processed as an administrator. It is recommended to disable the admin party in production environments. |