CouchDB

CouchDB Cheatsheet #

CouchDB is an open-source NoSQL database that uses a schema-free JSON document format and provides a RESTful HTTP API for interaction.

General Commands #

Command/OptionExampleDescription
curl http://localhost:5984/curl http://localhost:5984/Check if CouchDB is running
curl -X GET http://localhost:5984/_all_dbscurl -X GET http://localhost:5984/_all_dbsList all databases

Database Management #

Command/OptionExampleDescription
curl -X PUT http://localhost:5984/mydbcurl -X PUT http://localhost:5984/mydbCreate a new database
curl -X DELETE http://localhost:5984/mydbcurl -X DELETE http://localhost:5984/mydbDelete a database

Document Operations #

Command/OptionExampleDescription
curl -X POST http://localhost:5984/mydb -d '{"name":"John", "age":30}'curl -X POST http://localhost:5984/mydb -d '{"name":"John", "age":30}'Insert a new document
curl -X GET http://localhost:5984/mydb/<doc_id>curl -X GET http://localhost:5984/mydb/12345Retrieve a document by ID
curl -X PUT http://localhost:5984/mydb/<doc_id> -d '{"_rev":"<rev_id>", "name":"John", "age":31}'curl -X PUT http://localhost:5984/mydb/12345 -d '{"_rev":"1-2345", "name":"John", "age":31}'Update a document
curl -X DELETE http://localhost:5984/mydb/<doc_id>?rev=<rev_id>curl -X DELETE http://localhost:5984/mydb/12345?rev=1-2345Delete a document

Views and Queries #

Command/OptionExampleDescription
curl -X POST http://localhost:5984/mydb/_design/mydesign -d '{"views": {"by_age": {"map": "function(doc) { emit(doc.age, doc.name); }"}}}'curl -X POST http://localhost:5984/mydb/_design/mydesign -d '{"views": {"by_age": {"map": "function(doc) { emit(doc.age, doc.name); }"}}}'Create a design document with a view
curl -X GET http://localhost:5984/mydb/_design/mydesign/_view/by_age?key=30curl -X GET http://localhost:5984/mydb/_design/mydesign/_view/by_age?key=30Query a view by key

Replication #

Command/OptionExampleDescription
curl -X POST http://localhost:5984/_replicator -d '{"source": "source_db", "target": "target_db"}'curl -X POST http://localhost:5984/_replicator -d '{"source": "source_db", "target": "target_db"}'Set up a replication task

Security #

Command/OptionExampleDescription
curl -X PUT http://localhost:5984/_config/admins/<username> -d '"<password>"'curl -X PUT http://localhost:5984/_config/admins/admin -d '"secretpassword"'Create or update an admin user
curl -X GET http://localhost:5984/_session -u <username>:<password>curl -X GET http://localhost:5984/_session -u admin:secretpasswordAuthenticate as a user

Backup and Restore #

Command/OptionExampleDescription
curl -X GET http://localhost:5984/mydb/_all_docs?include_docs=true > backup.jsoncurl -X GET http://localhost:5984/mydb/_all_docs?include_docs=true > backup.jsonBackup a database to a JSON file
curl -X POST http://localhost:5984/mydb/_bulk_docs -d @backup.jsoncurl -X POST http://localhost:5984/mydb/_bulk_docs -d @backup.jsonRestore a database from a JSON file

This cheatsheet provides essential CouchDB commands and options for managing databases, documents, views, replication, security, and backups effectively.

CouchDB

Explore our comprehensive cheatsheets to enhance your knowledge and efficiency. Each cheatsheet provides detailed command options, examples, and descriptions to help you master various tools and technologies.