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/Option | Example | Description |
---|
curl http://localhost:5984/ | curl http://localhost:5984/ | Check if CouchDB is running |
curl -X GET http://localhost:5984/_all_dbs | curl -X GET http://localhost:5984/_all_dbs | List all databases |
Database Management
#
Command/Option | Example | Description |
---|
curl -X PUT http://localhost:5984/mydb | curl -X PUT http://localhost:5984/mydb | Create a new database |
curl -X DELETE http://localhost:5984/mydb | curl -X DELETE http://localhost:5984/mydb | Delete a database |
Document Operations
#
Command/Option | Example | Description |
---|
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/12345 | Retrieve 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-2345 | Delete a document |
Views and Queries
#
Command/Option | Example | Description |
---|
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=30 | curl -X GET http://localhost:5984/mydb/_design/mydesign/_view/by_age?key=30 | Query a view by key |
Replication
#
Command/Option | Example | Description |
---|
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/Option | Example | Description |
---|
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:secretpassword | Authenticate as a user |
Backup and Restore
#
Command/Option | Example | Description |
---|
curl -X GET http://localhost:5984/mydb/_all_docs?include_docs=true > backup.json | curl -X GET http://localhost:5984/mydb/_all_docs?include_docs=true > backup.json | Backup a database to a JSON file |
curl -X POST http://localhost:5984/mydb/_bulk_docs -d @backup.json | curl -X POST http://localhost:5984/mydb/_bulk_docs -d @backup.json | Restore a database from a JSON file |
This cheatsheet provides essential CouchDB commands and options for managing databases, documents, views, replication, security, and backups effectively.