1.3.1. /db

HEAD /{db}

Returns the HTTP Headers containing a minimal amount of information about the specified database. Since the response body is empty, using the HEAD method is a lightweight way to check if the database exists already or not.

  • Parameters

    • db – Database name

    Status Codes

Request:

  1. HEAD /test HTTP/1.1
  2. Host: localhost:5984

Response:

  1. HTTP/1.1 200 OK
  2. Cache-Control: must-revalidate
  3. Content-Type: application/json
  4. Date: Mon, 12 Aug 2013 01:27:41 GMT
  5. Server: CouchDB (Erlang/OTP)

GET /{db}

Gets information about the specified database.

  • Parameters

    • db – Database name

    Request Headers

    • Accept

      • application/json

      • text/plain

    Response Headers

    • Content-Type

      • application/json

      • text/plain; charset=utf-8

    Response JSON Object

    • cluster.n (number) – Replicas. The number of copies of every document.

    • cluster.q (number) – Shards. The number of range partitions.

    • cluster.r (number) – Read quorum. The number of consistent copies of a document that need to be read before a successful reply.

    • cluster.w (number) – Write quorum. The number of copies of a document that need to be written before a successful reply.

    • compact_running (boolean) – Set to true if the database compaction routine is operating on this database.

    • db_name (string) – The name of the database.

    • disk_format_version (number) – The version of the physical format used for the data when it is stored on disk.

    • doc_count (number) – A count of the documents in the specified database.

    • doc_del_count (number) – Number of deleted documents

    • instance_start_time (string) – Always "0". (Returned for legacy reasons.)

    • purge_seq (string) – An opaque string that describes the purge state of the database. Do not rely on this string for counting the number of purge operations.

    • sizes.active (number) – The size of live data inside the database, in bytes.

    • sizes.external (number) – The uncompressed size of database contents in bytes.

    • sizes.file (number) – The size of the database file on disk in bytes. Views indexes are not included in the calculation.

    • update_seq (string) – An opaque string that describes the state of the database. Do not rely on this string for counting the number of updates.

    • props.partitioned (boolean) – (optional) If present and true, this indicates that the database is partitioned.

    Status Codes

Request:

  1. GET /receipts HTTP/1.1
  2. Accept: application/json
  3. Host: localhost:5984

Response:

  1. HTTP/1.1 200 OK
  2. Cache-Control: must-revalidate
  3. Content-Length: 258
  4. Content-Type: application/json
  5. Date: Mon, 12 Aug 2013 01:38:57 GMT
  6. Server: CouchDB (Erlang/OTP)
  7. {
  8. "cluster": {
  9. "n": 3,
  10. "q": 8,
  11. "r": 2,
  12. "w": 2
  13. },
  14. "compact_running": false,
  15. "db_name": "receipts",
  16. "disk_format_version": 6,
  17. "doc_count": 6146,
  18. "doc_del_count": 64637,
  19. "instance_start_time": "0",
  20. "props": {},
  21. "purge_seq": 0,
  22. "sizes": {
  23. "active": 65031503,
  24. "external": 66982448,
  25. "file": 137433211
  26. },
  27. "update_seq": "292786-g1AAAAF..."
  28. }

PUT /{db}

Creates a new database. The database name {db} must be composed by following next rules:

  • Name must begin with a lowercase letter (a-z)

  • Lowercase characters (a-z)

  • Digits (0-9)

  • Any of the characters _, $, (, ), +, -, and /.

If you’re familiar with Regular Expressions, the rules above could be written as ^[a-z][a-z0-9_$()+/-]*$.

  • Parameters

    • db – Database name

    Query Parameters

    • q (integer) – Shards, aka the number of range partitions. Default is 8, unless overridden in the cluster config.

    • n (integer) – Replicas. The number of copies of the database in the cluster. The default is 3, unless overridden in the cluster config .

    • partitioned (boolean) – Whether to create a partitioned database. Default is false.

    Request Headers

    • Accept

      • application/json

      • text/plain

    Response Headers

    Response JSON Object

    • ok (boolean) – Operation status. Available in case of success

    • error (string) – Error type. Available if response code is 4xx

    • reason (string) – Error description. Available if response code is 4xx

    Status Codes

Request:

  1. PUT /db HTTP/1.1
  2. Accept: application/json
  3. Host: localhost:5984

Response:

  1. HTTP/1.1 201 Created
  2. Cache-Control: must-revalidate
  3. Content-Length: 12
  4. Content-Type: application/json
  5. Date: Mon, 12 Aug 2013 08:01:45 GMT
  6. Location: http://localhost:5984/db
  7. Server: CouchDB (Erlang/OTP)
  8. {
  9. "ok": true
  10. }

If we repeat the same request to CouchDB, it will response with 412 since the database already exists:

Request:

  1. PUT /db HTTP/1.1
  2. Accept: application/json
  3. Host: localhost:5984

Response:

  1. HTTP/1.1 412 Precondition Failed
  2. Cache-Control: must-revalidate
  3. Content-Length: 95
  4. Content-Type: application/json
  5. Date: Mon, 12 Aug 2013 08:01:16 GMT
  6. Server: CouchDB (Erlang/OTP)
  7. {
  8. "error": "file_exists",
  9. "reason": "The database could not be created, the file already exists."
  10. }

If an invalid database name is supplied, CouchDB returns response with 400:

Request:

  1. PUT /_db HTTP/1.1
  2. Accept: application/json
  3. Host: localhost:5984

Request:

  1. HTTP/1.1 400 Bad Request
  2. Cache-Control: must-revalidate
  3. Content-Length: 194
  4. Content-Type: application/json
  5. Date: Mon, 12 Aug 2013 08:02:10 GMT
  6. Server: CouchDB (Erlang/OTP)
  7. {
  8. "error": "illegal_database_name",
  9. "reason": "Name: '_db'. Only lowercase characters (a-z), digits (0-9), and any of the characters _, $, (, ), +, -, and / are allowed. Must begin with a letter."
  10. }

DELETE /{db}

Deletes the specified database, and all the documents and attachments contained within it.

Note

To avoid deleting a database, CouchDB will respond with the HTTP status code 400 when the request URL includes a ?rev= parameter. This suggests that one wants to delete a document but forgot to add the document id to the URL.

  • Parameters

    • db – Database name

    Request Headers

    • Accept

      • application/json

      • text/plain

    Response Headers

    • Content-Type

      • application/json

      • text/plain; charset=utf-8

    Response JSON Object

    • ok (boolean) – Operation status

    Status Codes

    • 200 OK – Database removed successfully (quorum is met and database is deleted by at least one node)

    • 202 Accepted – Accepted (deleted by at least one of the nodes, quorum is not met yet)

    • 400 Bad Request – Invalid database name or forgotten document id by accident

    • 401 Unauthorized – CouchDB Server Administrator privileges required

    • 404 Not Found – Database doesn’t exist or invalid database name

Request:

  1. DELETE /db HTTP/1.1
  2. Accept: application/json
  3. Host: localhost:5984

Response:

  1. HTTP/1.1 200 OK
  2. Cache-Control: must-revalidate
  3. Content-Length: 12
  4. Content-Type: application/json
  5. Date: Mon, 12 Aug 2013 08:54:00 GMT
  6. Server: CouchDB (Erlang/OTP)
  7. {
  8. "ok": true
  9. }

POST /{db}

Creates a new document in the specified database, using the supplied JSON document structure.

If the JSON structure includes the _id field, then the document will be created with the specified document ID.

If the _id field is not specified, a new unique ID will be generated, following whatever UUID algorithm is configured for that server.

  • Parameters

    • db – Database name

    Request Headers

    Query Parameters

    • batch (string) – Stores document in batch mode Possible values: ok. Optional

    Response Headers

    Response JSON Object

    • id (string) – Document ID

    • ok (boolean) – Operation status

    • rev (string) – Revision info

    Status Codes

Request:

  1. POST /db HTTP/1.1
  2. Accept: application/json
  3. Content-Length: 81
  4. Content-Type: application/json
  5. {
  6. "servings": 4,
  7. "subtitle": "Delicious with fresh bread",
  8. "title": "Fish Stew"
  9. }

Response:

  1. HTTP/1.1 201 Created
  2. Cache-Control: must-revalidate
  3. Content-Length: 95
  4. Content-Type: application/json
  5. Date: Tue, 13 Aug 2013 15:19:25 GMT
  6. Location: http://localhost:5984/db/ab39fe0993049b84cfa81acd6ebad09d
  7. Server: CouchDB (Erlang/OTP)
  8. {
  9. "id": "ab39fe0993049b84cfa81acd6ebad09d",
  10. "ok": true,
  11. "rev": "1-9c65296036141e575d32ba9c034dd3ee"
  12. }

1.3.1.1. Specifying the Document ID

The document ID can be specified by including the _id field in the JSON of the submitted record. The following request will create the same document with the ID FishStew.

Request:

  1. POST /db HTTP/1.1
  2. Accept: application/json
  3. Content-Length: 98
  4. Content-Type: application/json
  5. {
  6. "_id": "FishStew",
  7. "servings": 4,
  8. "subtitle": "Delicious with fresh bread",
  9. "title": "Fish Stew"
  10. }

Response:

  1. HTTP/1.1 201 Created
  2. Cache-Control: must-revalidate
  3. Content-Length: 71
  4. Content-Type: application/json
  5. Date: Tue, 13 Aug 2013 15:19:25 GMT
  6. ETag: "1-9c65296036141e575d32ba9c034dd3ee"
  7. Location: http://localhost:5984/db/FishStew
  8. Server: CouchDB (Erlang/OTP)
  9. {
  10. "id": "FishStew",
  11. "ok": true,
  12. "rev": "1-9c65296036141e575d32ba9c034dd3ee"
  13. }

1.3.1.2. Batch Mode Writes

You can write documents to the database at a higher rate by using the batch option. This collects document writes together in memory (on a per-user basis) before they are committed to disk. This increases the risk of the documents not being stored in the event of a failure, since the documents are not written to disk immediately.

Batch mode is not suitable for critical data, but may be ideal for applications such as log data, when the risk of some data loss due to a crash is acceptable.

To use batch mode, append the batch=ok query argument to the URL of a POST /{db}, PUT /{db}/{docid}, or DELETE /{db}/{docid} request. The CouchDB server will respond with an HTTP 202 Accepted response code immediately.

Note

Creating or updating documents with batch mode doesn’t guarantee that all documents will be successfully stored on disk. For example, individual documents may not be saved due to conflicts, rejection by validation function or by other reasons, even if overall the batch was successfully submitted.

Request:

  1. POST /db?batch=ok HTTP/1.1
  2. Accept: application/json
  3. Content-Length: 98
  4. Content-Type: application/json
  5. {
  6. "_id": "FishStew",
  7. "servings": 4,
  8. "subtitle": "Delicious with fresh bread",
  9. "title": "Fish Stew"
  10. }

Response:

  1. HTTP/1.1 202 Accepted
  2. Cache-Control: must-revalidate
  3. Content-Length: 28
  4. Content-Type: application/json
  5. Date: Tue, 13 Aug 2013 15:19:25 GMT
  6. Location: http://localhost:5984/db/FishStew
  7. Server: CouchDB (Erlang/OTP)
  8. {
  9. "id": "FishStew",
  10. "ok": true
  11. }