CouchDB ist eine dokumentenorientierte NoSQL-Datenbank des Apache-Projekts. Sie speichert JSON-Dokumente und wird komplett über eine HTTP-REST-API bedient — das Kommandozeilen-Werkzeug ist daher schlicht curl gegen den Port 5984.
Grundlagen
Die API liefert JSON-Antworten. Ohne Authentifizierung läuft CouchDB im Admin-Party-Modus; mit Admin-Konto lautet die URL http://user:pass@localhost:5984.
curl http://localhost:5984/ # Server-Info
curl http://localhost:5984/_all_dbs # Datenbanken auflisten
curl -X PUT http://localhost:5984/mydb # Datenbank anlegen
curl -X DELETE http://localhost:5984/mydb # Datenbank löschen
Dokumente verwalten
curl -X PUT http://localhost:5984/mydb/doc1 -H "Content-Type: application/json" -d '{"title": "Erster Eintrag", "tags": ["wiki"]}'
curl http://localhost:5984/mydb/doc1 # Dokument lesen
curl -X DELETE "http://localhost:5984/mydb/doc1?rev=1-abc" # Löschen braucht _rev
Jedes Dokument trägt eine _id und eine _rev (Revisionsnummer). Updates senden die aktuelle _rev mit — CouchDB erzwingt so optimistische Nebenläufigkeit (MVCC).
Abfragen und Replikation
curl "http://localhost:5984/mydb/_all_docs?include_docs=true" # alle Dokumente
curl -X POST http://localhost:5984/mydb/_find -H "Content-Type: application/json" -d '{"selector": {"tags": "wiki"}}' # Mango-Query
curl -X POST http://localhost:5984/_replicate -H "Content-Type: application/json" -d '{"source": "mydb", "target": "backup"}' # Replikation
Mango (seit CouchDB 2.0) erlaubt deklarative JSON-Suchabfragen mit selector. Für komplexe Auswertungen definiert man MapReduce-Views in Design-Dokumenten (_design/app). Die Replikation ist inkrementell und idempotent — sie eignet sich damit auch für Backups.
Praxis-Tipps
- Fauxton (Web-UI) ist unter
/_utilserreichbar. _bulk_docslegt viele Dokumente in einem Request an.- Periodische
_compact-Aufrufe verkleinern die Datenbankdateien.
Verwandte Grundlagen: MongoDB-Befehle, NoSQL-Datenbank, JSON und die neue Schwester DynamoDB-Befehle.