1.4.1. /db/doc
¶
-
HEAD
/{db}/{docid}
¶ 返回包含有关指定文档的最小信息量的HTTP头。该方法支持与 :get:`/{{db}}/{{docid}}` 方法,但只返回头信息(包括文档大小和作为ETag的修订)。
这个 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}
¶ 通过指定的
docid
从指定的db
. 除非您请求特定修订,否则将始终返回文档的最新版本。参数: - 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
响应头: - Content-Type --
- application/json
- multipart/related
- multipart/mixed
- text/plain; charset=utf-8
- ETag -- 双引号文档的修订标记。检索冲突相关信息时不可用
- Transfer-Encoding --
chunked
. Available if requested with query parameteropen_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}
¶ 这个 PUT 方法创建新的命名文档,或创建现有文档的新修订版。不像 :post:`/{{db}}` ,必须在请求URL中指定文档ID。
更新现有文档时,当前文档修订版本必须包含在文档中(即请求正文),作为
rev
查询参数,或在If-Match
请求头。参数: - db -- 数据库名称
- docid -- 文档ID
请求标头: - Accept --
- application/json
- text/plain
- Content-Type --
- application/json
- multipart/related
- If-Match -- 文件的修订。替代品 rev 查询参数或文档键。 可选的
查询参数: - rev (string) -- 如果更新现有文档,则为文档的修订版本。替代
If-Match
页眉或文档密钥。 可选 - batch (string) -- 将文档存储在 batch mode . 可能值:
ok
. 可选的 - new_edits (boolean) -- 防止插入 conflicting document 。可能的值:
true
(默认)和false
。如果false
,一个结构齐全的_rev
必须包含在文档中。new_edits=false
被复制器用来将文档插入到目标数据库中,即使这会导致冲突。 可选 , The ``false`` value is intended for use only by the replicator.
响应头: - Content-Type --
- application/json
- text/plain; charset=utf-8
- multipart/related
- ETag -- 引用文件的新版本
- Location -- 文档URI
响应JSON对象: - id (string) -- 文档ID
- ok (boolean) -- 运行状态
- rev (string) -- 修订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}
¶ 通过添加字段将指定文档标记为已删除
_deleted
用价值true
. 具有此字段的文档将不再在请求中返回,而是保留在数据库中。必须提供当前(最新)版本,方法是使用rev
参数或使用 If-Match 标题以指定修订。注解
CouchDB没有完全删除指定的文档。相反,它会在墓碑上留下关于文档的非常基本的信息。逻辑删除是必需的,以便可以跨数据库复制删除操作。
参数: - db -- 数据库名称
- docid -- 文档ID
请求标头: 查询参数: - rev (string) -- 实际文件修订
- batch (string) -- 将文档存储在 batch mode 可能值:
ok
. 可选的
响应头: - Content-Type --
- application/json
- text/plain; charset=utf-8
- ETag -- 双引号文件的新修订
响应JSON对象: - id (string) -- 文档ID
- ok (boolean) -- 运行状态
- rev (string) -- 修订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
或者,代替
rev
可以使用的查询参数 If-Match 标题: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和目标文档修订版(可选)。看到了吗 Copying to an Existing Document .
- If-Match -- 源文档的修订版本。替代
rev
查询参数
查询参数: - rev (string) -- 要复制的修订。 可选的
- batch (string) -- 将文档存储在 batch mode 可能值:
ok
. 可选的
响应头: - Content-Type --
- application/json
- text/plain; charset=utf-8
- ETag -- 双引号文件的新修订
- Location -- 文档URI
响应JSON对象: - id (string) -- 文档文档ID
- ok (boolean) -- 运行状态
- rev (string) -- 修订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编码的内容。如果使用以下查询参数请求附件内容,则可用:
attachments=true
查询文档时attachments=true&include_docs=true
when querying a changes feed or a viewatts_since
.
消化 ( 一串 ):内容哈希摘要。它以声明散列类型的前缀开始 (
md5-
)并继续使用Base64编码的哈希摘要encoded_length ( 数 ):压缩的附件大小(字节)。在以下情况下可用
content_type
是在list of compressible types
添加附件并指定以下查询参数时:att_encoding_info=true
查询文档时att_encoding_info=true&include_docs=true
when querying a changes feed or a view
编码 ( 一串 ):压缩编解码器。在以下情况下可用
content_type
是在list of compressible types
添加附件并指定以下查询参数时:att_encoding_info=true
查询文档时att_encoding_info=true&include_docs=true
when querying a changes feed or a view
长度 ( 数 ):实际附件大小(字节)。如果要求附件内容,则不可用
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 标题 :put:`/{{db}}/{{docid}}` 请求到 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. 获取修订列表¶
您可以通过添加 revs=true
请求URL的参数:
请求 :
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的最新修订(按最新修订顺序排列)
- 开始 ( 数 ):最新版本的前缀号
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 ( 一串 ):完整修订字符串
- 地位 ( 一串 ):修订的状态。可能是:
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. 从特定版本复制¶
复制 from 特定版本,使用 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"
}