Database / CouchDB Interview Questions
What is the CouchDB HTTP REST API and how do you perform basic CRUD operations?
CouchDB maps every database operation to a standard HTTP method and URL. The server root is typically http://localhost:5984. No driver installation is required — curl or any HTTP client works directly.
# Create a database
curl -X PUT http://admin:pass@localhost:5984/inventory
# {"ok":true}
# Create a document with a specific _id (PUT)
curl -X PUT http://admin:pass@localhost:5984/inventory/item:001 \
-H "Content-Type: application/json" \
-d '{"type":"item","name":"Widget","stock":150}'
# {"ok":true,"id":"item:001","rev":"1-3c6a8..."}
# Create a document with auto-generated _id (POST)
curl -X POST http://admin:pass@localhost:5984/inventory \
-H "Content-Type: application/json" \
-d '{"type":"item","name":"Gadget","stock":30}'
# Read a document
curl http://admin:pass@localhost:5984/inventory/item:001
# Returns JSON with _id and _rev
# Update — _rev is mandatory in the body
curl -X PUT http://admin:pass@localhost:5984/inventory/item:001 \
-H "Content-Type: application/json" \
-d '{"_rev":"1-3c6a8...","type":"item","name":"Widget","stock":200}'
# {"ok":true,"id":"item:001","rev":"2-9b4f1..."}
# Delete — pass _rev as query param
curl -X DELETE "http://admin:pass@localhost:5984/inventory/item:001?rev=2-9b4f1..."
# {"ok":true,"id":"item:001","rev":"3-d2e79..."}
# Bulk upsert
curl -X POST http://admin:pass@localhost:5984/inventory/_bulk_docs \
-H "Content-Type: application/json" \
-d '{"docs":[{"type":"item","name":"Part-A"},{"type":"item","name":"Part-B"}]}'
HTTP status codes follow REST conventions: 201 Created on success, 200 OK for reads, 404 Not Found, and 409 Conflict when the supplied _rev does not match the server's current revision. The _bulk_docs endpoint accepts thousands of documents per request, dramatically reducing round-trips for bulk loads.
More Related questions...