DEV Community

Cover image for Apache CouchDB cheatsheet
Jordan Soo Yen Yih
Jordan Soo Yen Yih

Posted on

Apache CouchDB cheatsheet

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

  1. Check CouchDB is running
GET / 
Enter fullscreen mode Exit fullscreen mode

Or

GET /_up
Enter fullscreen mode Exit fullscreen mode
  1. Check CouchDB's running tasks
GET /_active_tasks
Enter fullscreen mode Exit fullscreen mode
  1. Get all databases
GET /_all_dbs
Enter fullscreen mode Exit fullscreen mode
  1. Access the built-in Fauxton administration interface
GET /_utils
Enter fullscreen mode Exit fullscreen mode

Databases

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

* Create database
PUT /{YOUR_DATABASE_NAME}

* Delete database
DELETE /{YOUR_DATABASE_NAME}
Enter fullscreen mode Exit fullscreen mode
  1. Create a new document in the specified database.
POST /{YOUR_DATABASE_NAME}
Enter fullscreen mode Exit fullscreen mode
  1. List all documents in the database
GET /{YOUR_DATABASE_NAME}/_all_docs
Enter fullscreen mode Exit fullscreen mode
  1. Query several documents in bulk
POST /{YOUR_DATABASE_NAME}/_bulk_get
Enter fullscreen mode Exit fullscreen mode
  1. Create / Update documents in bulk
POST /{YOUR_DATABASE_NAME}/_bulk_docs
Enter fullscreen mode Exit fullscreen mode
  1. Find documents with JSON querying syntax (Mango Query)
POST /{YOUR_DATABASE_NAME}/_find
Enter fullscreen mode Exit fullscreen mode
  1. Setup database security
* Get security object
GET /{YOUR_DATABASE_NAME}/_security

* Set security object
PUT /{YOUR_DATABASE_NAME}/_security
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode
  1. 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}
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode
  1. 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}
Enter fullscreen mode Exit fullscreen mode
  1. Execute an Update Function
POST /{YOUR_DATABASE_NAME}/_design/{DESIGN_DOCUMENT_ID}/_update/{FUNCTION_NAME}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode

Thank you for reading

Above is the list of useful CouchDB HTTP API that use frequently by the developers. There are more actually but above are good enough for a normal project. Hope you find these resources useful.

Top comments (0)