1.4.1. /db/doc
¶
- HEAD /{db}/{docid}¶
返回包含有关指定文档的少量信息的 HTTP 标头。该方法支持与
GET /{db}/{docid}
方法相同的查询参数,但只返回标头信息(包括文档大小和修订版本作为 ETag)。The ETag 标头显示所请求文档的当前修订版本,而 Content-Length 指定数据的长度,如果文档以完整形式请求。
添加任何查询参数(参见
GET /{db}/{docid}
),则生成的 HTTP 标头将对应于将返回的内容。- 参数:
db – 数据库名称
docid – 文档 ID
- 请求标头:
If-None-Match – 文档的双引号修订版本标记
- 响应标头:
Content-Length – 文档大小
ETag – 文档的双引号修订版本标记
- 状态代码:
200 OK – 文档存在
304 Not Modified – 文档自指定修订版本以来未修改
401 Unauthorized – 需要读取权限
404 Not Found – 文档未找到
请求:
HEAD /db/SpaghettiWithMeatballs HTTP/1.1 Accept: application/json Host: localhost:5984
响应:
HTTP/1.1 200 OK Cache-Control: must-revalidate Content-Length: 660 Content-Type: application/json Date: Tue, 13 Aug 2013 21:35:37 GMT ETag: "12-151bb8678d45aaa949ec3698ef1c7e78" Server: CouchDB (Erlang/OTP)
- GET /{db}/{docid}¶
从指定的
db
中返回指定docid
的文档。除非您请求特定修订版本,否则始终返回文档的最新修订版本。- 参数:
db – 数据库名称
docid – 文档 ID
- 请求标头:
Accept –
application/json
multipart/related
multipart/mixed
text/plain
If-None-Match – 文档的双引号修订版本标记
- 查询参数:
attachments (boolean) – 在响应中包含附件主体。默认值为
false
att_encoding_info (boolean) – 如果特定附件已压缩,则在附件存根中包含编码信息。默认值为
false
。atts_since (array) – 仅包含自指定修订版本以来的附件。不包含指定修订版本的附件。可选
conflicts (boolean) – 包含有关文档中冲突的信息。默认值为
false
deleted_conflicts (boolean) – 包含有关已删除的冲突修订版本的信息。默认值为
false
latest (boolean) – 强制检索最新的“叶”修订版本,无论请求了哪个 rev。默认值为
false
local_seq (boolean) – 包含文档的最后更新序列。默认值为
false
meta (boolean) – 作用与指定所有
conflicts
、deleted_conflicts
和revs_info
查询参数相同。默认值为false
open_revs (array) – 检索指定叶修订版本的文档。此外,它接受
all
作为值以返回所有叶修订版本。可选rev (string) – 检索指定修订版本的文档。可选
revs (boolean) – 包含所有已知文档修订版本的列表。默认值为
false
revs_info (boolean) – 包含所有已知文档修订版本的详细信息。默认值为
false
- 响应标头:
application/json
multipart/related
multipart/mixed
text/plain; charset=utf-8
ETag – 文档的双引号修订版本标记。在检索与冲突相关的信息时不可用
Transfer-Encoding –
chunked
。如果使用查询参数open_revs
请求,则可用
- 响应 JSON 对象:
_id (string) – 文档 ID
_rev (string) – 修订版本 MVCC 标记
_deleted (boolean) – 删除标志。如果文档已删除,则可用
_attachments (object) – 附件的存根。如果文档有任何附件,则可用
_conflicts (array) – 冲突修订版本的列表。如果使用
conflicts=true
查询参数请求,则可用_deleted_conflicts (array) – 已删除的冲突修订版本的列表。如果使用
deleted_conflicts=true
查询参数请求,则可用_local_seq (string) – 文档在当前数据库中的更新序列。如果使用
local_seq=true
查询参数请求,则可用_revs_info (array) – 包含有关本地修订版本及其状态的信息的对象列表。如果使用
open_revs
查询参数请求,则可用_revisions (object) – 本地修订版本标记的列表,不带。如果使用
revs=true
查询参数请求,则可用
- 状态代码:
200 OK – 请求已成功完成
304 Not Modified – 文档自指定修订版本以来未修改
400 Bad Request – 请求或修订版本的格式无效
401 Unauthorized – 需要读取权限
404 Not Found – 文档未找到
请求:
GET /recipes/SpaghettiWithMeatballs HTTP/1.1 Accept: application/json Host: localhost:5984
响应:
HTTP/1.1 200 OK Cache-Control: must-revalidate Content-Length: 660 Content-Type: application/json Date: Tue, 13 Aug 2013 21:35:37 GMT ETag: "1-917fa2381192822767f010b95b45325b" Server: CouchDB (Erlang/OTP) { "_id": "SpaghettiWithMeatballs", "_rev": "1-917fa2381192822767f010b95b45325b", "description": "An Italian-American dish that usually consists of spaghetti, tomato sauce and meatballs.", "ingredients": [ "spaghetti", "tomato sauce", "meatballs" ], "name": "Spaghetti with meatballs" }
- PUT /{db}/{docid}¶
The PUT 方法创建新的命名文档,或创建现有文档的新修订版本。与
POST /{db}
不同,您必须在请求 URL 中指定文档 ID。更新现有文档时,必须在文档(即请求正文)中包含当前文档修订版本,作为
rev
查询参数,或在If-Match
请求标头中包含。- 参数:
db – 数据库名称
docid – 文档 ID
- 请求标头:
- 查询参数:
- 响应标头:
- 响应 JSON 对象:
id (字符串) – 文档 ID
ok (布尔值) – 操作状态
rev (字符串) – 修订版 MVCC 令牌
- 状态代码:
201 Created – 文档已创建并存储在磁盘上
202 Accepted – 文档数据已接受,但尚未存储在磁盘上
400 Bad Request – 无效的请求正文或参数
401 Unauthorized – 需要写入权限
404 Not Found – 指定的数据库或文档 ID 不存在
409 Conflict – 具有指定 ID 的文档已存在,或者指定的修订版不是目标文档的最新修订版
请求:
PUT /recipes/SpaghettiWithMeatballs HTTP/1.1 Accept: application/json Content-Length: 196 Content-Type: application/json Host: localhost:5984 { "description": "An Italian-American dish that usually consists of spaghetti, tomato sauce and meatballs.", "ingredients": [ "spaghetti", "tomato sauce", "meatballs" ], "name": "Spaghetti with meatballs" }
响应:
HTTP/1.1 201 Created Cache-Control: must-revalidate Content-Length: 85 Content-Type: application/json Date: Wed, 14 Aug 2013 20:31:39 GMT ETag: "1-917fa2381192822767f010b95b45325b" Location: http://localhost:5984/recipes/SpaghettiWithMeatballs Server: CouchDB (Erlang/OTP) { "id": "SpaghettiWithMeatballs", "ok": true, "rev": "1-917fa2381192822767f010b95b45325b" }
- DELETE /{db}/{docid}¶
通过添加一个值为
true
的_deleted
字段,将指定的文档标记为已删除。具有此字段的文档将不再在请求中返回,但会保留在数据库中。您必须提供当前(最新)修订版,方法是使用rev
参数或使用If-Match 标头来指定修订版。注意
CouchDB 不会完全删除指定的文档。相反,它会留下一个墓碑,其中包含有关文档的非常基本的信息。墓碑是必需的,以便删除操作可以在数据库之间复制。
另请参阅
- 参数:
db – 数据库名称
docid – 文档 ID
- 请求标头:
- 查询参数:
rev (字符串) – 实际文档的修订版
batch (字符串) – 将文档存储在批处理模式 中。可能的值:
ok
。可选
- 响应标头:
application/json
text/plain; charset=utf-8
ETag – 带双引号的文档的新修订版
- 响应 JSON 对象:
id (字符串) – 文档 ID
ok (布尔值) – 操作状态
rev (字符串) – 修订版 MVCC 令牌
- 状态代码:
200 OK – 文档已成功删除
202 Accepted – 请求已接受,但更改尚未存储在磁盘上
400 Bad Request – 无效的请求正文或参数
401 Unauthorized – 需要写入权限
404 Not Found – 指定的数据库或文档 ID 不存在
409 Conflict – 指定的修订版不是目标文档的最新修订版
请求:
DELETE /recipes/FishStew?rev=1-9c65296036141e575d32ba9c034dd3ee HTTP/1.1 Accept: application/json Host: localhost:5984
或者,您可以使用If-Match 标头,而不是
rev
查询参数DELETE /recipes/FishStew HTTP/1.1 Accept: application/json If-Match: 1-9c65296036141e575d32ba9c034dd3ee Host: localhost:5984
响应:
HTTP/1.1 200 OK Cache-Control: must-revalidate Content-Length: 71 Content-Type: application/json Date: Wed, 14 Aug 2013 12:23:13 GMT ETag: "2-056f5f44046ecafc08a2bc2b9c229e20" Server: CouchDB (Erlang/OTP) { "id": "FishStew", "ok": true, "rev": "2-056f5f44046ecafc08a2bc2b9c229e20" }
- COPY /{db}/{docid}¶
COPY(非标准 HTTP)将现有文档复制到新的或现有的文档。只能在同一个数据库内复制文档。
源文档在请求行上指定,请求的Destination 标头指定目标文档。
- 参数:
db – 数据库名称
docid – 文档 ID
- 请求标头:
Accept –
application/json
text/plain
Destination – 目标文档。必须包含目标文档 ID,以及可选的目标文档修订版(如果复制到现有文档)。请参阅复制到现有文档。
If-Match – 源文档的修订版。作为
rev
查询参数的替代方案
- 查询参数:
rev (字符串) – 要从中复制的修订版。可选
batch (字符串) – 将文档存储在批处理模式 中。可能的值:
ok
。可选
- 响应标头:
- 响应 JSON 对象:
id (字符串) – 文档文档 ID
ok (布尔值) – 操作状态
rev (字符串) – 修订版 MVCC 令牌
- 状态代码:
201 Created – 文档已成功创建
202 Accepted – 请求已接受,但更改尚未存储在磁盘上
400 Bad Request – 无效的请求正文或参数
401 Unauthorized – 需要读取或写入权限
404 Not Found – 指定的数据库、文档 ID 或修订版不存在
409 Conflict – 具有指定 ID 的文档已存在,或者指定的修订版不是目标文档的最新修订版
请求:
COPY /recipes/SpaghettiWithMeatballs HTTP/1.1 Accept: application/json Destination: SpaghettiWithMeatballs_Italian Host: localhost:5984
响应:
HTTP/1.1 201 Created Cache-Control: must-revalidate Content-Length: 93 Content-Type: application/json Date: Wed, 14 Aug 2013 14:21:00 GMT ETag: "1-e86fdf912560c2321a5fcefc6264e6d9" Location: http://localhost:5984/recipes/SpaghettiWithMeatballs_Italian Server: CouchDB (Erlang/OTP) { "id": "SpaghettiWithMeatballs_Italian", "ok": true, "rev": "1-e86fdf912560c2321a5fcefc6264e6d9" }
1.4.1.1. 附件¶
如果文档包含附件,则返回的结构将包含与文档关联的附件的摘要,但不包含附件数据本身。
返回的文档的 JSON 将包含_attachments
字段,其中包含一个或多个附件定义。
_attachments
对象键是附件名称,而值是具有以下结构的信息对象
content_type (字符串):附件 MIME 类型
data (字符串):Base64 编码的内容。如果使用以下查询参数请求附件内容,则可用
digest (字符串):内容哈希摘要。它以宣布哈希类型的前缀开头(
md5-
),并以 Base64 编码的哈希摘要继续encoded_length (数字):压缩附件的大小(以字节为单位)。如果
content_type
位于list of compressible types
中(在添加附件时),并且指定了以下查询参数,则可用encoding (字符串):压缩编解码器。如果
content_type
位于list of compressible types
中(在添加附件时),并且指定了以下查询参数,则可用length (数字):实际附件的大小(以字节为单位)。如果请求附件内容,则不可用
revpos (数字):添加附件时的修订版编号
stub (布尔值):如果对象包含存根信息且没有内容,则具有
true
值。否则在响应中省略
1.4.1.1.1. 基本附件信息¶
请求:
GET /recipes/SpaghettiWithMeatballs HTTP/1.1
Accept: application/json
Host: localhost:5984
响应:
HTTP/1.1 200 OK
Cache-Control: must-revalidate
Content-Length: 660
Content-Type: application/json
Date: Tue, 13 Aug 2013 21:35:37 GMT
ETag: "5-fd96acb3256302bf0dd2f32713161f2a"
Server: CouchDB (Erlang/OTP)
{
"_attachments": {
"grandma_recipe.txt": {
"content_type": "text/plain",
"digest": "md5-Ids41vtv725jyrN7iUvMcQ==",
"length": 1872,
"revpos": 4,
"stub": true
},
"my_recipe.txt": {
"content_type": "text/plain",
"digest": "md5-198BPPNiT5fqlLxoYYbjBA==",
"length": 85,
"revpos": 5,
"stub": true
},
"photo.jpg": {
"content_type": "image/jpeg",
"digest": "md5-7Pv4HW2822WY1r/3WDbPug==",
"length": 165504,
"revpos": 2,
"stub": true
}
},
"_id": "SpaghettiWithMeatballs",
"_rev": "5-fd96acb3256302bf0dd2f32713161f2a",
"description": "An Italian-American dish that usually consists of spaghetti, tomato sauce and meatballs.",
"ingredients": [
"spaghetti",
"tomato sauce",
"meatballs"
],
"name": "Spaghetti with meatballs"
}
1.4.1.1.2. 检索附件内容¶
可以使用attachments=true
查询参数检索包含所有附加文件内容的文档
请求:
GET /db/pixel?attachments=true HTTP/1.1
Accept: application/json
Host: localhost:5984
响应:
HTTP/1.1 200 OK
Cache-Control: must-revalidate
Content-Length: 553
Content-Type: application/json
Date: Wed, 14 Aug 2013 11:32:40 GMT
ETag: "4-f1bcae4bf7bbb92310079e632abfe3f4"
Server: CouchDB (Erlang/OTP)
{
"_attachments": {
"pixel.gif": {
"content_type": "image/gif",
"data": "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7",
"digest": "md5-2JdGiI2i2VELZKnwMers1Q==",
"revpos": 2
},
"pixel.png": {
"content_type": "image/png",
"data": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAAXNSR0IArs4c6QAAAANQTFRFAAAAp3o92gAAAAF0Uk5TAEDm2GYAAAABYktHRACIBR1IAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3QgOCx8VHgmcNwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=",
"digest": "md5-Dgf5zxgGuchWrve73evvGQ==",
"revpos": 3
}
},
"_id": "pixel",
"_rev": "4-f1bcae4bf7bbb92310079e632abfe3f4"
}
或者使用atts_since
查询参数检索自特定修订版以来的附加文件内容
请求:
GET /recipes/SpaghettiWithMeatballs?atts_since=[%224-874985bc28906155ba0e2e0538f67b05%22] HTTP/1.1
Accept: application/json
Host: localhost:5984
响应:
HTTP/1.1 200 OK
Cache-Control: must-revalidate
Content-Length: 760
Content-Type: application/json
Date: Tue, 13 Aug 2013 21:35:37 GMT
ETag: "5-fd96acb3256302bf0dd2f32713161f2a"
Server: CouchDB (Erlang/OTP)
{
"_attachments": {
"grandma_recipe.txt": {
"content_type": "text/plain",
"digest": "md5-Ids41vtv725jyrN7iUvMcQ==",
"length": 1872,
"revpos": 4,
"stub": true
},
"my_recipe.txt": {
"content_type": "text/plain",
"data": "MS4gQ29vayBzcGFnaGV0dGkKMi4gQ29vayBtZWV0YmFsbHMKMy4gTWl4IHRoZW0KNC4gQWRkIHRvbWF0byBzYXVjZQo1LiAuLi4KNi4gUFJPRklUIQ==",
"digest": "md5-198BPPNiT5fqlLxoYYbjBA==",
"revpos": 5
},
"photo.jpg": {
"content_type": "image/jpeg",
"digest": "md5-7Pv4HW2822WY1r/3WDbPug==",
"length": 165504,
"revpos": 2,
"stub": true
}
},
"_id": "SpaghettiWithMeatballs",
"_rev": "5-fd96acb3256302bf0dd2f32713161f2a",
"description": "An Italian-American dish that usually consists of spaghetti, tomato sauce and meatballs.",
"ingredients": [
"spaghetti",
"tomato sauce",
"meatballs"
],
"name": "Spaghetti with meatballs"
}
1.4.1.1.2.1. 高效地检索多个附件¶
如上所述,使用attachments=true
检索文档将返回一个包含所有附件的大型 JSON 对象。当您的文档和文件较小时,这没问题,但如果您附加了更大的内容(如媒体文件(音频/视频)),则解析此类响应可能会非常昂贵。
为了解决此问题,CouchDB 允许以multipart/related 格式获取文档
请求:
GET /recipes/secret?attachments=true HTTP/1.1
Accept: multipart/related
Host: localhost:5984
响应:
HTTP/1.1 200 OK
Content-Length: 538
Content-Type: multipart/related; boundary="e89b3e29388aef23453450d10e5aaed0"
Date: Sat, 28 Sep 2013 08:08:22 GMT
ETag: "2-c1c6c44c4bc3c9344b037c8690468605"
Server: CouchDB (Erlang OTP)
--e89b3e29388aef23453450d10e5aaed0
Content-Type: application/json
{"_id":"secret","_rev":"2-c1c6c44c4bc3c9344b037c8690468605","_attachments":{"recipe.txt":{"content_type":"text/plain","revpos":2,"digest":"md5-HV9aXJdEnu0xnMQYTKgOFA==","length":86,"follows":true}}}
--e89b3e29388aef23453450d10e5aaed0
Content-Disposition: attachment; filename="recipe.txt"
Content-Type: text/plain
Content-Length: 86
1. Take R
2. Take E
3. Mix with L
4. Add some A
5. Serve with X
--e89b3e29388aef23453450d10e5aaed0--
在此响应中,文档仅包含附件存根信息,并且非常短,而所有附件都作为单独的实体出现,这减少了内存占用量和处理开销(您会注意到,附件内容以原始数据形式出现,而不是以 Base64 编码形式出现,对吧?)。
1.4.1.1.3. 检索附件编码信息¶
通过使用att_encoding_info=true
查询参数,您可以检索有关压缩附件大小和使用编解码器的信息。
请求:
GET /recipes/SpaghettiWithMeatballs?att_encoding_info=true HTTP/1.1
Accept: application/json
Host: localhost:5984
响应:
HTTP/1.1 200 OK
Cache-Control: must-revalidate
Content-Length: 736
Content-Type: application/json
Date: Tue, 13 Aug 2013 21:35:37 GMT
ETag: "5-fd96acb3256302bf0dd2f32713161f2a"
Server: CouchDB (Erlang/OTP)
{
"_attachments": {
"grandma_recipe.txt": {
"content_type": "text/plain",
"digest": "md5-Ids41vtv725jyrN7iUvMcQ==",
"encoded_length": 693,
"encoding": "gzip",
"length": 1872,
"revpos": 4,
"stub": true
},
"my_recipe.txt": {
"content_type": "text/plain",
"digest": "md5-198BPPNiT5fqlLxoYYbjBA==",
"encoded_length": 100,
"encoding": "gzip",
"length": 85,
"revpos": 5,
"stub": true
},
"photo.jpg": {
"content_type": "image/jpeg",
"digest": "md5-7Pv4HW2822WY1r/3WDbPug==",
"length": 165504,
"revpos": 2,
"stub": true
}
},
"_id": "SpaghettiWithMeatballs",
"_rev": "5-fd96acb3256302bf0dd2f32713161f2a",
"description": "An Italian-American dish that usually consists of spaghetti, tomato sauce and meatballs.",
"ingredients": [
"spaghetti",
"tomato sauce",
"meatballs"
],
"name": "Spaghetti with meatballs"
}
1.4.1.1.4. 创建多个附件¶
要使用单个请求创建包含多个附件的文档,您只需将 Base64 编码的附件数据内联到文档正文中
{
"_id":"multiple_attachments",
"_attachments":
{
"foo.txt":
{
"content_type":"text\/plain",
"data": "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
},
"bar.txt":
{
"content_type":"text\/plain",
"data": "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
}
}
}
或者,您可以以multipart/related 格式更有效地上传包含附件的文档。这避免了对附件进行 Base64 编码,从而节省了 CPU 和带宽。为此,请将Content-Type 标头设置为multipart/related。
第一个 MIME 正文是文档本身,它应该具有自己的Content-Type 为application/json"。它还应该包含一个_attachments
元数据对象,其中每个附件对象都有一个键follows
,其值为true
。
后续 MIME 正文是附件。
请求:
PUT /temp/somedoc HTTP/1.1
Accept: application/json
Content-Length: 372
Content-Type: multipart/related;boundary="abc123"
Host: localhost:5984
User-Agent: HTTPie/0.6.0
--abc123
Content-Type: application/json
{
"body": "This is a body.",
"_attachments": {
"foo.txt": {
"follows": true,
"content_type": "text/plain",
"length": 21
},
"bar.txt": {
"follows": true,
"content_type": "text/plain",
"length": 20
}
}
}
--abc123
this is 21 chars long
--abc123
this is 20 chars lon
--abc123--
响应:
HTTP/1.1 201 Created
Cache-Control: must-revalidate
Content-Length: 72
Content-Type: application/json
Date: Sat, 28 Sep 2013 09:13:24 GMT
ETag: "1-5575e26acdeb1df561bb5b70b26ba151"
Location: http://localhost:5984/temp/somedoc
Server: CouchDB (Erlang OTP)
{
"id": "somedoc",
"ok": true,
"rev": "1-5575e26acdeb1df561bb5b70b26ba151"
}
1.4.1.2. 获取修订版列表¶
您可以通过在请求 URL 中添加 revs=true
参数来获取给定文档的修订列表。
请求:
GET /recipes/SpaghettiWithMeatballs?revs=true HTTP/1.1
Accept: application/json
Host: localhost:5984
响应:
HTTP/1.1 200 OK
Cache-Control: must-revalidate
Content-Length: 584
Content-Type: application/json
Date: Wed, 14 Aug 2013 11:38:26 GMT
ETag: "5-fd96acb3256302bf0dd2f32713161f2a"
Server: CouchDB (Erlang/OTP)
{
"_id": "SpaghettiWithMeatballs",
"_rev": "8-6f5ad8db0f34af24a6e0984cd1a6cfb9",
"_revisions": {
"ids": [
"6f5ad8db0f34af24a6e0984cd1a6cfb9",
"77fba3a059497f51ec99b9b478b569d2",
"136813b440a00a24834f5cb1ddf5b1f1",
"fd96acb3256302bf0dd2f32713161f2a",
"874985bc28906155ba0e2e0538f67b05",
"0de77a37463bf391d14283e626831f2e",
"d795d1b924777732fdea76538c558b62",
"917fa2381192822767f010b95b45325b"
],
"start": 8
},
"description": "An Italian-American dish that usually consists of spaghetti, tomato sauce and meatballs.",
"ingredients": [
"spaghetti",
"tomato sauce",
"meatballs"
],
"name": "Spaghetti with meatballs"
}
返回的 JSON 结构包含原始文档,包括一个 _revisions
结构,其中包含以下形式的修订信息。
ids (数组): 有效修订 ID 的数组,按反序排列(最新修订在最前面)。
start (数字): 最新修订的前缀数字。
1.4.1.3. 获取扩展修订历史记录¶
您可以通过在查询中提供 revs_info
参数来获取有关给定文档的修订的更多信息。
请求:
GET /recipes/SpaghettiWithMeatballs?revs_info=true HTTP/1.1
Accept: application/json
Host: localhost:5984
响应:
HTTP/1.1 200 OK
Cache-Control: must-revalidate
Content-Length: 802
Content-Type: application/json
Date: Wed, 14 Aug 2013 11:40:55 GMT
Server: CouchDB (Erlang/OTP)
{
"_id": "SpaghettiWithMeatballs",
"_rev": "8-6f5ad8db0f34af24a6e0984cd1a6cfb9",
"_revs_info": [
{
"rev": "8-6f5ad8db0f34af24a6e0984cd1a6cfb9",
"status": "available"
},
{
"rev": "7-77fba3a059497f51ec99b9b478b569d2",
"status": "deleted"
},
{
"rev": "6-136813b440a00a24834f5cb1ddf5b1f1",
"status": "available"
},
{
"rev": "5-fd96acb3256302bf0dd2f32713161f2a",
"status": "missing"
},
{
"rev": "4-874985bc28906155ba0e2e0538f67b05",
"status": "missing"
},
{
"rev": "3-0de77a37463bf391d14283e626831f2e",
"status": "missing"
},
{
"rev": "2-d795d1b924777732fdea76538c558b62",
"status": "missing"
},
{
"rev": "1-917fa2381192822767f010b95b45325b",
"status": "missing"
}
],
"description": "An Italian-American dish that usually consists of spaghetti, tomato sauce and meatballs.",
"ingredients": [
"spaghetti",
"tomato sauce",
"meatballs"
],
"name": "Spaghetti with meatballs"
}
返回的文档包含 _revs_info
字段,其中包含扩展的修订信息,包括每个修订的可用性和状态。此数组字段包含具有以下结构的对象。
rev (字符串): 完整的修订字符串。
status (字符串): 修订的状态。可能是以下之一。
available
: 修订可通过 rev 查询参数检索。missing
: 修订不可用。deleted
: 修订属于已删除的文档。
1.4.1.4. 获取特定修订¶
要获取特定修订,请在请求中使用 rev
参数,并指定完整的修订编号。将返回文档的指定修订,包括一个 _rev
字段,指定请求的修订。
请求:
GET /recipes/SpaghettiWithMeatballs?rev=6-136813b440a00a24834f5cb1ddf5b1f1 HTTP/1.1
Accept: application/json
Host: localhost:5984
响应:
HTTP/1.1 200 OK
Cache-Control: must-revalidate
Content-Length: 271
Content-Type: application/json
Date: Wed, 14 Aug 2013 11:40:55 GMT
Server: CouchDB (Erlang/OTP)
{
"_id": "SpaghettiWithMeatballs",
"_rev": "6-136813b440a00a24834f5cb1ddf5b1f1",
"description": "An Italian-American dish that usually consists of spaghetti, tomato sauce and meatballs.",
"ingredients": [
"spaghetti",
"tomato sauce",
"meatballs"
],
"name": "Spaghetti with meatballs"
}
1.4.1.4.1. 检索已删除的文档¶
CouchDB 实际上不会通过 DELETE /{db}/{docid}
删除文档。相反,它会留下一个墓碑,其中包含有关文档的非常基本的信息。如果您只是 GET /{db}/{docid}
CouchDB 返回 404 Not Found 响应。
请求:
GET /recipes/FishStew HTTP/1.1
Accept: application/json
Host: localhost:5984
响应:
HTTP/1.1 404 Object Not Found
Cache-Control: must-revalidate
Content-Length: 41
Content-Type: application/json
Date: Wed, 14 Aug 2013 12:23:27 GMT
Server: CouchDB (Erlang/OTP)
{
"error": "not_found",
"reason": "deleted"
}
但是,您可以通过使用 rev
查询参数以及 GET /{db}/{docid}
请求来检索文档的墓碑。
请求:
GET /recipes/FishStew?rev=2-056f5f44046ecafc08a2bc2b9c229e20 HTTP/1.1
Accept: application/json
Host: localhost:5984
响应:
HTTP/1.1 200 OK
Cache-Control: must-revalidate
Content-Length: 79
Content-Type: application/json
Date: Wed, 14 Aug 2013 12:30:22 GMT
ETag: "2-056f5f44046ecafc08a2bc2b9c229e20"
Server: CouchDB (Erlang/OTP)
{
"_deleted": true,
"_id": "FishStew",
"_rev": "2-056f5f44046ecafc08a2bc2b9c229e20"
}
1.4.1.5. 更新现有文档¶
要更新现有文档,您必须在 _rev
参数中指定当前修订编号。
请求:
PUT /recipes/SpaghettiWithMeatballs HTTP/1.1
Accept: application/json
Content-Length: 258
Content-Type: application/json
Host: localhost:5984
{
"_rev": "1-917fa2381192822767f010b95b45325b",
"description": "An Italian-American dish that usually consists of spaghetti, tomato sauce and meatballs.",
"ingredients": [
"spaghetti",
"tomato sauce",
"meatballs"
],
"name": "Spaghetti with meatballs",
"serving": "hot"
}
或者,您可以在请求的 If-Match
HTTP 标头中提供当前修订编号。
PUT /recipes/SpaghettiWithMeatballs HTTP/1.1
Accept: application/json
Content-Length: 258
Content-Type: application/json
If-Match: 1-917fa2381192822767f010b95b45325b
Host: localhost:5984
{
"description": "An Italian-American dish that usually consists of spaghetti, tomato sauce and meatballs.",
"ingredients": [
"spaghetti",
"tomato sauce",
"meatballs"
],
"name": "Spaghetti with meatballs",
"serving": "hot"
}
响应:
HTTP/1.1 201 Created
Cache-Control: must-revalidate
Content-Length: 85
Content-Type: application/json
Date: Wed, 14 Aug 2013 20:33:56 GMT
ETag: "2-790895a73b63fb91dd863388398483dd"
Location: http://localhost:5984/recipes/SpaghettiWithMeatballs
Server: CouchDB (Erlang/OTP)
{
"id": "SpaghettiWithMeatballs",
"ok": true,
"rev": "2-790895a73b63fb91dd863388398483dd"
}
1.4.1.6. 从特定修订复制¶
要从特定版本复制,请在查询字符串中使用 rev
参数或 If-Match。
请求:
COPY /recipes/SpaghettiWithMeatballs HTTP/1.1
Accept: application/json
Destination: SpaghettiWithMeatballs_Original
If-Match: 1-917fa2381192822767f010b95b45325b
Host: localhost:5984
响应:
HTTP/1.1 201 Created
Cache-Control: must-revalidate
Content-Length: 93
Content-Type: application/json
Date: Wed, 14 Aug 2013 14:21:00 GMT
ETag: "1-917fa2381192822767f010b95b45325b"
Location: http://localhost:5984/recipes/SpaghettiWithMeatballs_Original
Server: CouchDB (Erlang/OTP)
{
"id": "SpaghettiWithMeatballs_Original",
"ok": true,
"rev": "1-917fa2381192822767f010b95b45325b"
}
1.4.1.7. 复制到现有文档¶
要复制到现有文档,您必须通过将 rev
参数附加到 Destination 标头字符串来指定目标文档的当前修订字符串。
请求:
COPY /recipes/SpaghettiWithMeatballs?rev=8-6f5ad8db0f34af24a6e0984cd1a6cfb9 HTTP/1.1
Accept: application/json
Destination: SpaghettiWithMeatballs_Original?rev=1-917fa2381192822767f010b95b45325b
Host: localhost:5984
响应:
HTTP/1.1 201 Created
Cache-Control: must-revalidate
Content-Length: 93
Content-Type: application/json
Date: Wed, 14 Aug 2013 14:21:00 GMT
ETag: "2-62e778c9ec09214dd685a981dcc24074""
Location: http://localhost:5984/recipes/SpaghettiWithMeatballs_Original
Server: CouchDB (Erlang/OTP)
{
"id": "SpaghettiWithMeatballs_Original",
"ok": true,
"rev": "2-62e778c9ec09214dd685a981dcc24074"
}