CouchDB Cheat Sheet
CouchDB is a NoSQL database that uses a document-oriented approach for storing and retrieving data. This cheatsheet is a quick reference guide for common CouchDB operations and commands.
Table of Contents
No. | Operation/Command | Syntax |
---|---|---|
1. | Install CouchDB | sudo apt-get install couchdb |
2. | Start/Stop CouchDB | sudo service couchdb start sudo service couchdb stop |
3. | Access CouchDB Fauxton (Web UI) | http://localhost:5984/_utils/ |
4. | Create a Database | curl -X PUT http://localhost:5984/mydatabase |
5. | List All Databases | curl http://localhost:5984/_all_dbs |
6. | Delete a Database | curl -X DELETE http://localhost:5984/mydatabase |
7. | Insert a Document | curl -X POST http://localhost:5984/mydatabase -d '{"key": "value"}' |
8. | Retrieve a Document | curl http://localhost:5984/mydatabase/document_id |
9. | Update a Document | curl -X PUT http://localhost:5984/mydatabase/document_id -d '{"new_key": "new_value"}' |
10. | Delete a Document | curl -X DELETE http://localhost:5984/mydatabase/document_id?rev=document_revision |
11. | Bulk Insert Documents | curl -X POST http://localhost:5984/mydatabase/_bulk_docs -d '{"docs": [{"key": "value1"}, {"key": "value2"}]}' |
12. | Create a Design Document | curl -X PUT http://localhost:5984/mydatabase/_design/mydesign -d '{"views": {"myview": {"map": "function(doc) { emit(doc._id, null); }"}}}' |
13. | Query a View | curl http://localhost:5984/mydatabase/_design/mydesign/_view/myview |
14. | Replication - Create a Replication Document | curl -X POST http://localhost:5984/_replicate -d '{"source": "http://source_db_url", "target": "http://target_db_url"}' |
15. | Compact a Database | curl -X POST http://localhost:5984/mydatabase/_compact |
16. | View Database Information | curl http://localhost:5984/mydatabase |
17. | Create a User | curl -X PUT http://localhost:5984/_users/org.couchdb.user:newuser -d '{"name": "newuser", "password": "password", "roles": [], "type": "user"}' |
18. | List All Users | curl http://localhost:5984/_users/_all_docs |
19. | Delete a User | curl -X DELETE http://localhost:5984/_users/org.couchdb.user:newuser?rev=user_revision |
20. | Security - Create an API Key | curl -X POST http://localhost:5984/_api/v2/db/mydatabase/_security/apiKeys -H "Authorization: Basic base64(username:password)" |
21. | View Active Tasks | curl http://localhost:5984/_active_tasks |
22. | Configure CouchDB | Edit the local.ini configuration file. |
23. | Backup a Database | curl -X POST http://localhost:5984/_replicate -d '{"source": "http://localhost:5984/mydatabase", "target": "backup_location"}' |
24. | Restore a Database | curl -X POST http://localhost:5984/_replicate -d '{"source": "backup_location", "target": "http://localhost:5984/mydatabase"}' |
25. | Check CouchDB Version | curl http://localhost:5984 |
26. | View Logs | tail -f /opt/couchdb/var/log/couchdb/couch.log |
27. | Enable CORS | Edit the local.ini configuration file and add the necessary CORS configurations. |
28. | Change Admin Password | curl -X PUT http://localhost:5984/_config/admins/admin -d '"new_password"' |
29. | MapReduce Example | Create map and reduce functions in a design document. |
30. | Compact View | curl -X POST http://localhost:5984/mydatabase/_compact/mydesign/myview |