Apache CouchDB cheatsheet
Tags: database, couchdb, beginners
Published: 2021-10-25 14:48:45

Apache CouchDB is an open source NoSQL document database that collects and stores data in JSON-based document formats. Unlike relational databases, CouchDB uses a schema-free data model, which simplifies record management across various computing devices, mobile phones and web browsers.

In this post, I will focus on a few useful HTTP API which are use more frequently by the developers.


##Index
* [Server](#server)
* [Databases](#databases)
* [Documents](#documents)
* [Design Documents](design-documents)
* [Bonus](bonus)

---

##Server
1. Check CouchDB is running
```
GET / 
```
Or
```
GET /_up
```

2. Check CouchDB's running tasks
```
GET /_active_tasks
```

3. Get all databases
```
GET /_all_dbs
```

4. Access the built-in Fauxton administration interface
```
GET /_utils
```


##Databases
1. Database Operations.
```
* Get database
GET /{YOUR_DATABASE_NAME}

* Create database
PUT /{YOUR_DATABASE_NAME}

* Delete database
DELETE /{YOUR_DATABASE_NAME}
```

2. Create a new document in the specified database.
```
POST /{YOUR_DATABASE_NAME}
```

3. List all documents in the database
```
GET /{YOUR_DATABASE_NAME}/_all_docs
```

4. Query several documents in bulk
```
POST /{YOUR_DATABASE_NAME}/_bulk_get
```

5. Create / Update documents in bulk
```
POST /{YOUR_DATABASE_NAME}/_bulk_docs
```

6. Find documents with JSON querying syntax (Mango Query)
```
POST /{YOUR_DATABASE_NAME}/_find
```

7. Setup database security
```
* Get security object
GET /{YOUR_DATABASE_NAME}/_security

* Set security object
PUT /{YOUR_DATABASE_NAME}/_security
```


##Documents
1. Document CRUD
```
* Get a specific document
GET /{YOUR_DATABASE_NAME}/{DOCUMENT_ID}

* Create or Update a specific document
PUT /{YOUR_DATABASE_NAME}/{DOCUMENT_ID}

* Delete a specific document
DELETE /{YOUR_DATABASE_NAME}/{DOCUMENT_ID}
```

2. File Attachment CRUD.
```
* Get a file attachment associated with the document
GET /{YOUR_DATABASE_NAME}/{DOCUMENT_ID}/{ATTACHMENT_NAME}

* Upload an attachment to the specified document
PUT /{YOUR_DATABASE_NAME}/{DOCUMENT_ID}/{ATTACHMENT_NAME}

* Delete an attachment from the specified document
DELETE /{YOUR_DATABASE_NAME}/{DOCUMENT_ID}/{ATTACHMENT_NAME}
```


##Design Documents
1. Design Document CRUD
```
* Get a design document
GET /{YOUR_DATABASE_NAME}/_design/{DESIGN_DOCUMENT_ID}

* Create a new design document
PUT /{YOUR_DATABASE_NAME}/_design/{DESIGN_DOCUMENT_ID}

* Delete a design document
PUT /{YOUR_DATABASE_NAME}/_design/{DESIGN_DOCUMENT_ID}
```

2. Execute a View function
```
GET /{YOUR_DATABASE_NAME}/_design/{DESIGN_DOCUMENT_ID}/_view/{VIEW_NAME}

* Execute a view function with query string parameters
POST /{YOUR_DATABASE_NAME}/_design/{DESIGN_DOCUMENT_ID}/_view/{VIEW_NAME}
```

3. Execute an Update Function
```
POST /{YOUR_DATABASE_NAME}/_design/{DESIGN_DOCUMENT_ID}/_update/{FUNCTION_NAME}
```

##Bonus
1. Authentication
For CouchDB v3.0+, currently supports 4 type of authentication methods (Basic, Cookie, Proxy, JWT). Every methods are interacting with the same API endpoint.
```
GET /_session
POST /_session
```

2. Database changes

This is an interesting and powerful API which allows you to get a sorted list of changes made to documents in the specified database.
```
GET /{YOUR_DATABASE_NAME}/_changes

* For query parameter
POST /{YOUR_DATABASE_NAME}/_changes
```

---

##Thank you for reading
Above is the list of useful CouchDB HTTP API that use frequently by the developers. There are [more](https://docs.couchdb.org/en/stable/api/index.html#) actually but above are good enough for a normal project. Hope you find these resources useful.