~ubuntu-branches/ubuntu/trusty/couchdb/trusty

« back to all changes in this revision

Viewing changes to share/doc/build/html/_sources/api/documents.txt

  • Committer: Package Import Robot
  • Author(s): Jason Gerard DeRose
  • Date: 2013-12-01 16:55:05 UTC
  • mfrom: (1.3.5)
  • Revision ID: package-import@ubuntu.com-20131201165505-for2toyl58mhzwj2
Tags: 1.5.0-0ubuntu1
* New upstream release (LP: #1254371)
* Don't include `couchdb` info page in `couchdb-bin` binary package as it
  provides no meaningful benefit over the `couchdb` man page (note this change
  means we don't need to add a Build-Depends on `install-info` for Trusty)
* Remove Build-Depends: texlive-latex-base, texlive-latex-recommended,
  texlive-latex-extra, texlive-fonts-recommended, texinfo (as documentation
  thus produced doesn't get included in the binary packages anyway)
* debian/rules: don't call ./configure with --enable-strictness as we dropped
  Build-Depends on `texlive-*`, `texinfo`, plus didn't add `install-info` 
* Add Build-Depends: lsb-release (used for [vendor] info in default.ini)
* debian/rules: insert proper [vendor] info in default.ini (note this should
  be improved once there is a better mechanism upstream)
* debian/couchdb.upstart: start on filesystem and static-network-up,
  stop on deconfiguring-networking, plus add "author" line

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
.. Licensed under the Apache License, Version 2.0 (the "License"); you may not
2
 
.. use this file except in compliance with the License. You may obtain a copy of
3
 
.. the License at
4
 
..
5
 
..   http://www.apache.org/licenses/LICENSE-2.0
6
 
..
7
 
.. Unless required by applicable law or agreed to in writing, software
8
 
.. distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9
 
.. WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
 
.. License for the specific language governing permissions and limitations under
11
 
.. the License.
12
 
 
13
 
.. _api-doc:
14
 
 
15
 
================
16
 
Document Methods
17
 
================
18
 
 
19
 
The CouchDB API Server Document methods detail how to create, read,
20
 
update and delete documents within a database.
21
 
 
22
 
A list of the available methods and URL paths are provided below:
23
 
 
24
 
+--------+-------------------------+-------------------------------------------+
25
 
| Method | Path                    | Description                               |
26
 
+========+=========================+===========================================+
27
 
| POST   | /db                     | Create a new document                     |
28
 
+--------+-------------------------+-------------------------------------------+
29
 
| GET    | /db/doc                 | Returns the latest revision of the        |
30
 
|        |                         | document                                  |
31
 
+--------+-------------------------+-------------------------------------------+
32
 
| HEAD   | /db/doc                 | Returns bare information in the HTTP      |
33
 
|        |                         | Headers for the document                  |
34
 
+--------+-------------------------+-------------------------------------------+
35
 
| PUT    | /db/doc                 | Inserts a new document, or new version    |
36
 
|        |                         | of an existing document                   |
37
 
+--------+-------------------------+-------------------------------------------+
38
 
| DELETE | /db/doc                 | Deletes the document                      |
39
 
+--------+-------------------------+-------------------------------------------+
40
 
| COPY   | /db/doc                 | Copies the document                       |
41
 
+--------+-------------------------+-------------------------------------------+
42
 
| GET    | /db/doc/attachment      | Gets the attachment of a document         |
43
 
+--------+-------------------------+-------------------------------------------+
44
 
| PUT    | /db/doc/attachment      | Adds an attachment of a document          |
45
 
+--------+-------------------------+-------------------------------------------+
46
 
| DELETE | /db/doc/attachment      | Deletes an attachment of a document       |
47
 
+--------+-------------------------+-------------------------------------------+
48
 
 
49
 
``POST /db``
50
 
============
51
 
 
52
 
* **Method**: ``POST /db``
53
 
* **Request**: JSON of the new document
54
 
* **Response**: JSON with the committed document information
55
 
* **Admin Privileges Required**: no
56
 
* **Query Arguments**:
57
 
 
58
 
  * **Argument**: batch
59
 
 
60
 
    * **Description**:  Allow document store request to be batched with others
61
 
    * **Optional**: yes
62
 
    * **Type**: string
63
 
    * **Supported Values**: asd
64
 
    * **ok**: Enable
65
 
 
66
 
* **Return Codes**:
67
 
 
68
 
  * **201**:
69
 
    Document has been created successfully
70
 
  * **409**:
71
 
    Conflict - a document with the specified document ID already exists
72
 
 
73
 
Create a new document in the specified database, using the supplied JSON
74
 
document structure. If the JSON structure includes the ``_id`` field,
75
 
then the document will be created with the specified document ID. If the
76
 
``_id`` field is not specified, a new unique ID will be generated.
77
 
 
78
 
For example, you can generate a new document with a generated UUID using
79
 
the following request:
80
 
 
81
 
.. code-block:: http
82
 
 
83
 
    POST http://couchdb:5984/recipes/
84
 
    Content-Type: application/json
85
 
 
86
 
    {
87
 
       "servings" : 4,
88
 
       "subtitle" : "Delicious with fresh bread",
89
 
       "title" : "Fish Stew"
90
 
    }
91
 
 
92
 
The return JSON will specify the automatically generated ID and revision
93
 
information:
94
 
 
95
 
.. code-block:: javascript
96
 
 
97
 
    {
98
 
       "id" : "64575eef70ab90a2b8d55fc09e00440d",
99
 
       "ok" : true,
100
 
       "rev" : "1-9c65296036141e575d32ba9c034dd3ee"
101
 
    }
102
 
 
103
 
Specifying the Document ID
104
 
--------------------------
105
 
 
106
 
The document ID can be specified by including the ``_id`` field in the
107
 
JSON of the submitted record. The following request will create the same
108
 
document with the ID ``FishStew``:
109
 
 
110
 
.. code-block:: http
111
 
 
112
 
    POST http://couchdb:5984/recipes/
113
 
    Content-Type: application/json
114
 
 
115
 
    {
116
 
       "_id" : "FishStew",
117
 
       "servings" : 4,
118
 
       "subtitle" : "Delicious with fresh bread",
119
 
       "title" : "Fish Stew"
120
 
    }
121
 
 
122
 
The structure of the submitted document is as shown in the table below:
123
 
 
124
 
In either case, the returned JSON will specify the document ID, revision
125
 
ID, and status message:
126
 
 
127
 
.. code-block:: javascript
128
 
 
129
 
    {
130
 
       "id" : "FishStew",
131
 
       "ok" : true,
132
 
       "rev" : "1-9c65296036141e575d32ba9c034dd3ee"
133
 
    }
134
 
        
135
 
 
136
 
.. _api-batch-writes:
137
 
 
138
 
UUID generation algorithms
139
 
--------------------------
140
 
 
141
 
CouchDB supports a number of different UUID generation algorithms for use
142
 
in situations where a user-specified UUID does not make sense. These
143
 
can be set simply by `PUT http://couchdb:5984/_config/uuids/algorithm`.
144
 
 
145
 
 
146
 
+---------------+---------------------+------------------------------------+
147
 
| Algorithm     | Description         | Sample UUID                        |
148
 
+===============+=====================+====================================+
149
 
| random        | 128 bits of pure    | - 43febce5675468a5467fb5467ce9e6c0 |
150
 
|               | random awesomeness  |                                    |
151
 
+---------------+---------------------+------------------------------------+
152
 
| sequential    | monotonically       | - f755c413badf66b22941313f9f001e28 |
153
 
|               | increasing ids with | - f755c413badf66b22941313f9f0024ca |
154
 
|               | random increments   | - f755c413badf66b22941313f9f00332c |
155
 
+---------------+---------------------+------------------------------------+
156
 
| utc_random    | time since start of | - 04cfa405381205204f75100d0241ccc3 |
157
 
|               | epoch, as 14 hex    | - 04cfa4059c48e76e7c054bbe033dd8db |
158
 
|               | digits, followed by | - 04cfa405fce10b0df4c08f95e667cd2f |
159
 
|               | 18 random digits.   |                                    |
160
 
+---------------+---------------------+------------------------------------+
161
 
| utc_id        | time since start of | - 04cfa718b00848_i_am_in_yer_couch |
162
 
| & additional  | epoch, as 14 hex    | - 04cfa71d377aef_i_am_in_yer_couch |
163
 
| parameter     | digits, followed by | - 04cfa71e0deabd_i_am_in_yer_couch |
164
 
|               | utc_id_suffix.      |                                    |
165
 
+---------------+---------------------+------------------------------------+
166
 
 
167
 
.. Impact of UUID choices::
168
 
   The choice of UUID has a signficant impact on the layout of the B-tree,
169
 
   prior to compaction. For example, a sequential UUID algorithm during
170
 
   uploading thousands of documents, will avoid the need to rewrite many
171
 
   intermediate B-tree nodes. A random UUID algorithm may require rewriting
172
 
   intermediate nodes on a regular basis, with a corresponding decrease of
173
 
   throughput, and significant wasted space due to the append-only B-tree
174
 
   design. It is generally recommended to set your own UUIDs, or use the
175
 
   sequential algorithm unless you have a specific need and take into account
176
 
   the likely need for compaction to re-balance the B-tree and reclaim wasted
177
 
   space.
178
 
 
179
 
 
180
 
Batch Mode Writes
181
 
-----------------
182
 
 
183
 
You can write documents to the database at a higher rate by using the
184
 
batch option. This collects document writes together in memory (on a
185
 
user-by-user basis) before they are committed to disk. This increases
186
 
the risk of the documents not being stored in the event of a failure,
187
 
since the documents are not written to disk immediately.
188
 
 
189
 
To use the batched mode, append the ``batch=ok`` query argument to the
190
 
URL of the ``PUT`` or ``POST`` request. The CouchDB server will respond
191
 
with a 202 HTTP response code immediately.
192
 
 
193
 
Including Attachments
194
 
---------------------
195
 
 
196
 
You can include one or more attachments with a given document by
197
 
incorporating the attachment information within the JSON of the
198
 
document. This provides a simpler alternative to loading documents with
199
 
attachments than making a separate call (see :ref:`api-put-attachment`).
200
 
 
201
 
* **_id** (optional): Document ID
202
 
* **_rev** (optional): Revision ID (when updating an existing document)
203
 
* **_attachments** (optional): Document Attachment
204
 
 
205
 
  * **filename**: Attachment information
206
 
 
207
 
    * **content_type**: MIME Content type string
208
 
    * **data**: File attachment content, Base64 encoded
209
 
 
210
 
The ``filename`` will be the attachment name. For example, when sending
211
 
the JSON structure below:
212
 
 
213
 
.. code-block:: javascript
214
 
 
215
 
    {
216
 
       "_id" : "FishStew",
217
 
       "servings" : 4,
218
 
       "subtitle" : "Delicious with fresh bread",
219
 
       "title" : "Fish Stew"
220
 
       "_attachments" : {
221
 
          "styling.css" : {
222
 
             "content-type" : "text/css",
223
 
             "data" : "cCB7IGZvbnQtc2l6ZTogMTJwdDsgfQo=",
224
 
             },
225
 
       },
226
 
    }
227
 
        
228
 
 
229
 
The attachment ``styling.css`` can be accessed using
230
 
``/recipes/FishStew/styling.css``. For more information on attachments,
231
 
see :ref:`api-get-attachment`.
232
 
 
233
 
The document data embedded in to the structure must be encoded using
234
 
base64.
235
 
 
236
 
.. _api-get-doc:
237
 
 
238
 
``GET /db/doc``
239
 
===============
240
 
 
241
 
* **Method**: ``GET /db/doc``
242
 
* **Request**: None
243
 
* **Response**: Returns the JSON for the document
244
 
* **Admin Privileges Required**: no
245
 
* **Query Arguments**:
246
 
 
247
 
  * **Argument**: conflicts
248
 
 
249
 
    * **Description**: Returns the conflict tree for the document.
250
 
    * **Optional**: yes
251
 
    * **Type**: boolean
252
 
    * **Default**: false
253
 
    * **Supported Values**:
254
 
 
255
 
      * **true**: Includes the revisions
256
 
 
257
 
  * **Argument**: rev
258
 
 
259
 
    * **Description**: Specify the revision to return
260
 
    * **Optional**: yes
261
 
    * **Type**: string
262
 
    * **Supported Values**:
263
 
 
264
 
      * **true**: Includes the revisions
265
 
 
266
 
  * **Argument**: revs
267
 
 
268
 
    * **Description**:  Return a list of the revisions for the document
269
 
    * **Optional**: yes
270
 
    * **Type**: boolean
271
 
 
272
 
  * **Argument**: revs_info
273
 
 
274
 
    * **Description**: Return a list of detailed revision information for the
275
 
      document
276
 
    * **Optional**: yes
277
 
    * **Type**: boolean
278
 
    * **Supported Values**:
279
 
 
280
 
      * **true**: Includes the revisions
281
 
 
282
 
* **Return Codes**:
283
 
 
284
 
  * **200**:
285
 
    Document retrieved
286
 
  * **400**:
287
 
    The format of the request or revision was invalid
288
 
  * **404**:
289
 
    The specified document or revision cannot be found, or has been deleted
290
 
  * **409**:
291
 
    Conflict - a document with the specified document ID already exists
292
 
 
293
 
Returns the specified ``doc`` from the specified ``db``. For example, to
294
 
retrieve the document with the id ``FishStew`` you would send the
295
 
following request:
296
 
 
297
 
.. code-block:: http
298
 
 
299
 
    GET http://couchdb:5984/recipes/FishStew
300
 
    Content-Type: application/json
301
 
    Accept: application/json
302
 
 
303
 
The returned JSON is the JSON of the document, including the document ID
304
 
and revision number:
305
 
 
306
 
.. code-block:: javascript
307
 
 
308
 
    {
309
 
       "_id" : "FishStew",
310
 
       "_rev" : "3-a1a9b39ee3cc39181b796a69cb48521c",
311
 
       "servings" : 4,
312
 
       "subtitle" : "Delicious with a green salad",
313
 
       "title" : "Irish Fish Stew"
314
 
    }
315
 
        
316
 
 
317
 
Unless you request a specific revision, the latest revision of the
318
 
document will always be returned.
319
 
 
320
 
Attachments
321
 
-----------
322
 
 
323
 
If the document includes attachments, then the returned structure will
324
 
contain a summary of the attachments associated with the document, but
325
 
not the attachment data itself.
326
 
 
327
 
The JSON for the returned document will include the ``_attachments``
328
 
field, with one or more attachment definitions. For example:
329
 
 
330
 
.. code-block:: javascript
331
 
 
332
 
    {
333
 
       "_id" : "FishStew",
334
 
       "servings" : 4,
335
 
       "subtitle" : "Delicious with fresh bread",
336
 
       "title" : "Fish Stew"
337
 
       "_attachments" : {
338
 
          "styling.css" : {
339
 
             "stub" : true,
340
 
             "content-type" : "text/css",
341
 
             "length" : 783426,
342
 
             },
343
 
       },
344
 
    }
345
 
 
346
 
The format of the returned JSON is shown in the table below:
347
 
 
348
 
* **_id** (optional): Document ID
349
 
* **_rev** (optional): Revision ID (when updating an existing document)
350
 
* **_attachments** (optional): Document Attachment
351
 
 
352
 
  * **filename**: Attachment information
353
 
 
354
 
    * **content_type**: MIME Content type string
355
 
    * **length**: Length (bytes) of the attachment data
356
 
    * **revpos**: Revision where this attachment exists
357
 
    * **stub**: Indicates whether the attachment is a stub
358
 
 
359
 
Getting a List of Revisions
360
 
---------------------------
361
 
 
362
 
You can obtain a list of the revisions for a given document by adding
363
 
the ``revs=true`` parameter to the request URL. For example:
364
 
 
365
 
.. code-block:: http
366
 
 
367
 
    GET http://couchdb:5984/recipes/FishStew?revs=true
368
 
    Accept: application/json
369
 
 
370
 
The returned JSON structure includes the original document, including a
371
 
``_revisions`` structure that includes the revision information:
372
 
 
373
 
.. code-block:: javascript
374
 
 
375
 
    {
376
 
       "servings" : 4,
377
 
       "subtitle" : "Delicious with a green salad",
378
 
       "_id" : "FishStew",
379
 
       "title" : "Irish Fish Stew",
380
 
       "_revisions" : {
381
 
          "ids" : [
382
 
             "a1a9b39ee3cc39181b796a69cb48521c",
383
 
             "7c4740b4dcf26683e941d6641c00c39d",
384
 
             "9c65296036141e575d32ba9c034dd3ee"
385
 
          ],
386
 
          "start" : 3
387
 
       },
388
 
       "_rev" : "3-a1a9b39ee3cc39181b796a69cb48521c"
389
 
    }
390
 
 
391
 
* **_id** (optional): Document ID
392
 
* **_rev** (optional): Revision ID (when updating an existing document)
393
 
* **_revisions**: CouchDB Document Revisions
394
 
 
395
 
  * **ids** [array]: Array of valid revision IDs, in reverse order
396
 
    (latest first)
397
 
  * **start**: Prefix number for the latest revision
398
 
 
399
 
Obtaining an Extended Revision History
400
 
--------------------------------------
401
 
 
402
 
You can get additional information about the revisions for a given
403
 
document by supplying the ``revs_info`` argument to the query:
404
 
 
405
 
.. code-block:: http
406
 
 
407
 
    GET http://couchdb:5984/recipes/FishStew?revs_info=true
408
 
    Accept: application/json
409
 
 
410
 
This returns extended revision information, including the availability
411
 
and status of each revision:
412
 
 
413
 
.. code-block:: javascript
414
 
 
415
 
    {
416
 
       "servings" : 4,
417
 
       "subtitle" : "Delicious with a green salad",
418
 
       "_id" : "FishStew",
419
 
       "_revs_info" : [
420
 
          {
421
 
             "status" : "available",
422
 
             "rev" : "3-a1a9b39ee3cc39181b796a69cb48521c"
423
 
          },
424
 
          {
425
 
             "status" : "available",
426
 
             "rev" : "2-7c4740b4dcf26683e941d6641c00c39d"
427
 
          },
428
 
          {
429
 
             "status" : "available",
430
 
             "rev" : "1-9c65296036141e575d32ba9c034dd3ee"
431
 
          }
432
 
       ],
433
 
       "title" : "Irish Fish Stew",
434
 
       "_rev" : "3-a1a9b39ee3cc39181b796a69cb48521c"
435
 
    }
436
 
 
437
 
* **_id** (optional): Document ID
438
 
* **_rev** (optional): Revision ID (when updating an existing document)
439
 
* **_revs_info** [array]: CouchDB Document Extended Revision Info
440
 
 
441
 
  * **rev**: Full revision string
442
 
  * **status**: Status of the revision
443
 
 
444
 
Obtaining a Specific Revision
445
 
-----------------------------
446
 
 
447
 
To get a specific revision, use the ``rev`` argument to the request, and
448
 
specify the full revision number:
449
 
 
450
 
.. code-block:: http
451
 
 
452
 
    GET http://couchdb:5984/recipes/FishStew?rev=2-7c4740b4dcf26683e941d6641c00c39d
453
 
    Accept: application/json
454
 
 
455
 
The specified revision of the document will be returned, including a
456
 
``_rev`` field specifying the revision that was requested:
457
 
 
458
 
.. code-block:: javascript
459
 
 
460
 
    {
461
 
       "_id" : "FishStew",
462
 
       "_rev" : "2-7c4740b4dcf26683e941d6641c00c39d",
463
 
       "servings" : 4,
464
 
       "subtitle" : "Delicious with a green salad",
465
 
       "title" : "Fish Stew"
466
 
    }
467
 
 
468
 
``HEAD /db/doc``
469
 
================
470
 
 
471
 
* **Method**: ``HEAD /db/doc``
472
 
* **Request**: None
473
 
* **Response**: None
474
 
* **Admin Privileges Required**: no
475
 
* **Query Arguments**:
476
 
 
477
 
  * **Argument**: rev
478
 
 
479
 
    * **Description**:  Specify the revision to return
480
 
    * **Optional**: yes
481
 
    * **Type**: string
482
 
 
483
 
  * **Argument**: revs
484
 
 
485
 
    * **Description**:  Return a list of the revisions for the document
486
 
    * **Optional**: yes
487
 
    * **Type**: boolean
488
 
 
489
 
  * **Argument**: revs_info
490
 
 
491
 
    * **Description**:  Return a list of detailed revision information for the
492
 
      document
493
 
    * **Optional**: yes
494
 
    * **Type**: boolean
495
 
 
496
 
* **Return Codes**:
497
 
 
498
 
  * **404**:
499
 
    The specified document or revision cannot be found, or has been deleted
500
 
 
501
 
Returns the HTTP Headers containing a minimal amount of information
502
 
about the specified document. The method supports the same query
503
 
arguments as the ``GET`` method, but only the header information
504
 
(including document size, and the revision as an ETag), is returned. For
505
 
example, a simple ``HEAD`` request:
506
 
 
507
 
.. code-block:: http
508
 
 
509
 
    HEAD http://couchdb:5984/recipes/FishStew
510
 
    Content-Type: application/json
511
 
        
512
 
 
513
 
Returns the following HTTP Headers:
514
 
 
515
 
.. code-block:: javascript
516
 
 
517
 
    HTTP/1.1 200 OK
518
 
    Server: CouchDB/1.0.1 (Erlang OTP/R13B)
519
 
    Etag: "7-a19a1a5ecd946dad70e85233ba039ab2"
520
 
    Date: Fri, 05 Nov 2010 14:54:43 GMT
521
 
    Content-Type: text/plain;charset=utf-8
522
 
    Content-Length: 136
523
 
    Cache-Control: must-revalidate
524
 
 
525
 
The ``Etag`` header shows the current revision for the requested
526
 
document, and the ``Content-Length`` specifies the length of the data,
527
 
if the document were requested in full.
528
 
 
529
 
Adding any of the query arguments (as supported by ```GET```_ method),
530
 
then the resulting HTTP Headers will correspond to what would be
531
 
returned. Note that the current revision is not returned when the
532
 
``refs_info`` argument is used. For example:
533
 
 
534
 
.. code-block:: http
535
 
 
536
 
    HTTP/1.1 200 OK
537
 
    Server: CouchDB/1.0.1 (Erlang OTP/R13B)
538
 
    Date: Fri, 05 Nov 2010 14:57:16 GMT
539
 
    Content-Type: text/plain;charset=utf-8
540
 
    Content-Length: 609
541
 
    Cache-Control: must-revalidate
542
 
 
543
 
.. _api-put-doc:
544
 
 
545
 
``PUT /db/doc``
546
 
===============
547
 
 
548
 
* **Method**: ``PUT /db/doc``
549
 
* **Request**: JSON of the new document, or updated version of the existed
550
 
  document
551
 
* **Response**: JSON of the document ID and revision
552
 
* **Admin Privileges Required**: no
553
 
* **Query Arguments**:
554
 
 
555
 
  * **Argument**: batch
556
 
 
557
 
    * **Description**:  Allow document store request to be batched with others
558
 
    * **Optional**: yes
559
 
    * **Type**: string
560
 
    * **Supported Values**:
561
 
 
562
 
      * **ok**: Enable
563
 
 
564
 
* **HTTP Headers**
565
 
 
566
 
  * **Header**: ``If-Match``
567
 
 
568
 
    * **Description**: Current revision of the document for validation
569
 
    * **Optional**: yes
570
 
 
571
 
* **Return Codes**:
572
 
 
573
 
  * **201**:
574
 
    Document has been created successfully
575
 
  * **202**:
576
 
    Document accepted for writing (batch mode)
577
 
 
578
 
 
579
 
The ``PUT`` method creates a new named document, or creates a new
580
 
revision of the existing document. Unlike the ``POST`` method, you
581
 
must specify the document ID in the request URL.
582
 
 
583
 
For example, to create the document ``FishStew``, you would send the
584
 
following request:
585
 
 
586
 
.. code-block:: http
587
 
 
588
 
    PUT http://couchdb:5984/recipes/FishStew
589
 
    Content-Type: application/json
590
 
 
591
 
    {
592
 
      "servings" : 4,
593
 
      "subtitle" : "Delicious with fresh bread",
594
 
      "title" : "Fish Stew"
595
 
    }
596
 
 
597
 
The return type is JSON of the status, document ID,and revision number:
598
 
 
599
 
.. code-block:: javascript
600
 
 
601
 
    {
602
 
       "id" : "FishStew",
603
 
       "ok" : true,
604
 
       "rev" : "1-9c65296036141e575d32ba9c034dd3ee"
605
 
    }
606
 
 
607
 
Updating an Existing Document
608
 
-----------------------------
609
 
 
610
 
To update an existing document you must specify the current revision
611
 
number within the ``_rev`` parameter. For example:
612
 
 
613
 
.. code-block:: http
614
 
 
615
 
    PUT http://couchdb:5984/recipes/FishStew
616
 
    Content-Type: application/json
617
 
 
618
 
    {
619
 
      "_rev" : "1-9c65296036141e575d32ba9c034dd3ee",
620
 
      "servings" : 4,
621
 
      "subtitle" : "Delicious with fresh salad",
622
 
      "title" : "Fish Stew"
623
 
    }
624
 
 
625
 
Alternatively, you can supply the current revision number in the
626
 
``If-Match`` HTTP header of the request. For example:
627
 
 
628
 
.. code-block:: http
629
 
 
630
 
    PUT http://couchdb:5984/recipes/FishStew
631
 
    If-Match: 2-d953b18035b76f2a5b1d1d93f25d3aea
632
 
    Content-Type: application/json
633
 
 
634
 
    {
635
 
       "servings" : 4,
636
 
       "subtitle" : "Delicious with fresh salad",
637
 
       "title" : "Fish Stew"
638
 
    }
639
 
 
640
 
The JSON returned will include the updated revision number:
641
 
 
642
 
.. code-block:: javascript
643
 
 
644
 
    {
645
 
       "id" : "FishStew99",
646
 
       "ok" : true,
647
 
       "rev" : "2-d953b18035b76f2a5b1d1d93f25d3aea"
648
 
    }
649
 
 
650
 
For information on batched writes, which can provide improved
651
 
performance, see :ref:`api-batch-writes`.
652
 
 
653
 
.. _api-del-doc:
654
 
 
655
 
``DELETE /db/doc``
656
 
==================
657
 
 
658
 
* **Method**: ``DELETE /db/doc``
659
 
* **Request**: None
660
 
* **Response**: JSON of the deleted revision
661
 
* **Admin Privileges Required**: no
662
 
* **Query Arguments**:
663
 
 
664
 
  * **Argument**: rev
665
 
 
666
 
    * **Description**:  Current revision of the document for validation
667
 
    * **Optional**: yes
668
 
    * **Type**: string
669
 
 
670
 
* **HTTP Headers**
671
 
 
672
 
  * **Header**: ``If-Match``
673
 
 
674
 
    * **Description**: Current revision of the document for validation
675
 
    * **Optional**: yes
676
 
 
677
 
* **Return Codes**:
678
 
 
679
 
  * **409**:
680
 
    Revision is missing, invalid or not the latest
681
 
 
682
 
Deletes the specified document from the database. You must supply the
683
 
current (latest) revision, either by using the ``rev`` parameter to
684
 
specify the revision:
685
 
 
686
 
.. code-block:: http
687
 
 
688
 
    DELETE http://couchdb:5984/recipes/FishStew?rev=3-a1a9b39ee3cc39181b796a69cb48521c
689
 
    Content-Type: application/json
690
 
 
691
 
Alternatively, you can use ETags with the ``If-Match`` field:
692
 
 
693
 
.. code-block:: http
694
 
 
695
 
    DELETE http://couchdb:5984/recipes/FishStew
696
 
    If-Match: 3-a1a9b39ee3cc39181b796a69cb48521c
697
 
    Content-Type: application/json
698
 
        
699
 
 
700
 
The returned JSON contains the document ID, revision and status:
701
 
 
702
 
.. code-block:: javascript
703
 
 
704
 
    {
705
 
       "id" : "FishStew",
706
 
       "ok" : true,
707
 
       "rev" : "4-2719fd41187c60762ff584761b714cfb"
708
 
    }
709
 
 
710
 
.. note:: Note that deletion of a record increments the revision number. The
711
 
   use of a revision for deletion of the record allows replication of
712
 
   the database to correctly track the deletion in synchronized copies.
713
 
 
714
 
.. _api-copy-doc:
715
 
 
716
 
``COPY /db/doc``
717
 
================
718
 
 
719
 
* **Method**: ``COPY /db/doc``
720
 
* **Request**: None
721
 
* **Response**: JSON of the new document and revision
722
 
* **Admin Privileges Required**: no
723
 
* **Query Arguments**:
724
 
 
725
 
  * **Argument**: rev
726
 
 
727
 
    * **Description**:  Revision to copy from
728
 
    * **Optional**: yes
729
 
    * **Type**: string
730
 
 
731
 
* **HTTP Headers**
732
 
 
733
 
  * **Header**: ``Destination``
734
 
 
735
 
    * **Description**: Destination document (and optional revision)
736
 
    * **Optional**: no
737
 
 
738
 
* **Return Codes**:
739
 
 
740
 
  * **201**:
741
 
    Document has been copied and created successfully
742
 
  * **409**:
743
 
    Revision is missing, invalid or not the latest
744
 
 
745
 
The ``COPY`` command (which is non-standard HTTP) copies an existing
746
 
document to a new or existing document.
747
 
 
748
 
The source document is specified on the request line, with the
749
 
``Destination`` HTTP Header of the request specifying the target
750
 
document.
751
 
 
752
 
Copying a Document
753
 
------------------
754
 
 
755
 
You can copy the latest version of a document to a new document by
756
 
specifying the current document and target document:
757
 
 
758
 
.. code-block:: http
759
 
 
760
 
    COPY http://couchdb:5984/recipes/FishStew
761
 
    Content-Type: application/json
762
 
    Destination: IrishFishStew
763
 
 
764
 
The above request copies the document ``FishStew`` to the new document
765
 
``IrishFishStew``. The response is the ID and revision of the new
766
 
document.
767
 
 
768
 
.. code-block:: javascript
769
 
 
770
 
    {
771
 
       "id" : "IrishFishStew",
772
 
       "rev" : "1-9c65296036141e575d32ba9c034dd3ee"
773
 
    }
774
 
 
775
 
Copying from a Specific Revision
776
 
--------------------------------
777
 
 
778
 
To copy *from* a specific version, use the ``rev`` argument to the query
779
 
string:
780
 
 
781
 
.. code-block:: http
782
 
 
783
 
    COPY http://couchdb:5984/recipes/FishStew?rev=5-acfd32d233f07cea4b4f37daaacc0082
784
 
    Content-Type: application/json
785
 
    Destination: IrishFishStew
786
 
 
787
 
The new document will be created using the information in the specified
788
 
revision of the source document.
789
 
 
790
 
Copying to an Existing Document
791
 
-------------------------------
792
 
 
793
 
To copy to an existing document, you must specify the current revision
794
 
string for the target document, using the ``rev`` parameter to the
795
 
``Destination`` HTTP Header string. For example:
796
 
 
797
 
.. code-block:: http
798
 
 
799
 
    COPY http://couchdb:5984/recipes/FishStew
800
 
    Content-Type: application/json
801
 
    Destination: IrishFishStew?rev=1-9c65296036141e575d32ba9c034dd3ee
802
 
 
803
 
The return value will be the new revision of the copied document:
804
 
 
805
 
.. code-block:: javascript
806
 
 
807
 
    {
808
 
       "id" : "IrishFishStew",
809
 
       "rev" : "2-55b6a1b251902a2c249b667dab1c6692"
810
 
    }
811
 
 
812
 
.. _api-get-attachment:
813
 
 
814
 
``GET /db/doc/attachment``
815
 
==========================
816
 
 
817
 
* **Method**: ``GET /db/doc/attachment``
818
 
* **Request**: None
819
 
* **Response**: Returns the attachment data
820
 
* **Admin Privileges Required**: no
821
 
 
822
 
Returns the file attachment ``attachment`` associated with the document
823
 
``doc``. The raw data of the associated attachment is returned (just as
824
 
if you were accessing a static file. The returned HTTP ``Content-type``
825
 
will be the same as the content type set when the document attachment
826
 
was submitted into the database.
827
 
 
828
 
.. _api-put-attachment:
829
 
 
830
 
``PUT /db/doc/attachment``
831
 
==========================
832
 
 
833
 
* **Method**: ``PUT /db/doc/attachment``
834
 
* **Request**: Raw document data
835
 
* **Response**: JSON document status
836
 
* **Admin Privileges Required**: no
837
 
* **Query Arguments**:
838
 
 
839
 
  * **Argument**: rev
840
 
 
841
 
    * **Description**:  Current document revision
842
 
    * **Optional**: no
843
 
    * **Type**: string
844
 
 
845
 
* **HTTP Headers**
846
 
 
847
 
  * **Header**: ``Content-Length``
848
 
 
849
 
    * **Description**: Length (bytes) of the attachment being uploaded
850
 
    * **Optional**: no
851
 
 
852
 
  * **Header**: ``Content-Type``
853
 
 
854
 
    * **Description**: MIME type for the uploaded attachment
855
 
    * **Optional**: no
856
 
 
857
 
  * **Header**: ``If-Match``
858
 
 
859
 
    * **Description**: Current revision of the document for validation
860
 
    * **Optional**: yes
861
 
 
862
 
* **Return Codes**:
863
 
 
864
 
  * **201**:
865
 
    Attachment has been accepted
866
 
 
867
 
Upload the supplied content as an attachment to the specified document
868
 
(``doc``). The ``attachment`` name provided must be a URL encoded
869
 
string. You must also supply either the ``rev`` query argument or the
870
 
``If-Match`` HTTP header for validation, and the HTTP headers (to set
871
 
the attachment content type). The content type is used when the
872
 
attachment is requested as the corresponding content-type in the
873
 
returned document header.
874
 
 
875
 
For example, you could upload a simple text document using the following
876
 
request:
877
 
 
878
 
.. code-block:: http
879
 
 
880
 
    PUT http://couchdb:5984/recipes/FishStew/basic?rev=8-a94cb7e50ded1e06f943be5bfbddf8ca
881
 
    Content-Length: 10
882
 
    Content-Type: text/plain
883
 
 
884
 
    Roast it
885
 
 
886
 
Or by using the ``If-Match`` HTTP header:
887
 
 
888
 
.. code-block:: http
889
 
 
890
 
    PUT http://couchdb:5984/recipes/FishStew/basic
891
 
    If-Match: 8-a94cb7e50ded1e06f943be5bfbddf8ca
892
 
    Content-Length: 10
893
 
    Content-Type: text/plain
894
 
 
895
 
    Roast it
896
 
 
897
 
The returned JSON contains the new document information:
898
 
 
899
 
.. code-block:: javascript
900
 
 
901
 
    {
902
 
       "id" : "FishStew",
903
 
       "ok" : true,
904
 
       "rev" : "9-247bb19a41bfd9bfdaf5ee6e2e05be74"
905
 
    }
906
 
 
907
 
.. note:: Uploading an attachment updates the corresponding document revision.
908
 
   Revisions are tracked for the parent document, not individual
909
 
   attachments.
910
 
 
911
 
Updating an Existing Attachment
912
 
-------------------------------
913
 
 
914
 
Uploading an attachment using an existing attachment name will update
915
 
the corresponding stored content of the database. Since you must supply
916
 
the revision information to add an attachment to a document, this serves
917
 
as validation to update the existing attachment.
918
 
 
919
 
``DELETE /db/doc/attachment``
920
 
=============================
921
 
 
922
 
* **Method**: ``DELETE /db/doc/attachment``
923
 
* **Request**: None
924
 
* **Response**: JSON status
925
 
* **Admin Privileges Required**: no
926
 
* **Query Arguments**:
927
 
 
928
 
  * **Argument**: rev
929
 
 
930
 
    * **Description**:  Current document revision
931
 
    * **Optional**: no
932
 
    * **Type**: string
933
 
 
934
 
* **HTTP Headers**
935
 
 
936
 
  * **Header**: ``If-Match``
937
 
 
938
 
    * **Description**: Current revision of the document for validation
939
 
    * **Optional**: yes
940
 
 
941
 
* **Return Codes**:
942
 
 
943
 
  * **200**:
944
 
    Attachment deleted successfully
945
 
  * **409**:
946
 
    Supplied revision is incorrect or missing
947
 
 
948
 
Deletes the attachment ``attachment`` to the specified ``doc``. You must
949
 
supply the ``rev`` argument with the current revision to delete the
950
 
attachment.
951
 
 
952
 
For example to delete the attachment ``basic`` from the recipe
953
 
``FishStew``:
954
 
 
955
 
.. code-block:: http
956
 
 
957
 
    DELETE http://couchdb:5984/recipes/FishStew/basic?rev=9-247bb19a41bfd9bfdaf5ee6e2e05be74
958
 
    Content-Type: application/json
959
 
 
960
 
        
961
 
 
962
 
The returned JSON contains the updated revision information:
963
 
 
964
 
.. code-block:: javascript
965
 
 
966
 
    {
967
 
       "id" : "FishStew",
968
 
       "ok" : true,
969
 
       "rev" : "10-561bf6b1e27615cee83d1f48fa65dd3e"
970
 
    }
971
 
 
972
 
.. _JSON object: #table-couchdb-api-db_db-json-changes
973
 
.. _POST: #couchdb-api-dbdoc_db_post