1.5.7. /db/_design/design-doc/_show/show-name

警告

Show函数在couchdb3.0中被弃用,并将在couchdb4.0中被删除。

GET /{db}/_design/{ddoc}/_show/{func}
POST /{db}/_design/{ddoc}/_show/{func}

适用范围 show functionnull 文档。

请求和响应参数取决于函数实现。

参数:
  • db -- 数据库名称
  • ddoc -- 设计文件名称
  • func -- 显示函数名
响应头:
  • ETag -- 响应签名
查询参数:
  • format (string) -- 返回响应的格式。使用人 provides() 功能
状态代码:

功能

function(doc, req) {
    if (!doc) {
        return {body: "no doc"}
    } else {
        return {body: doc.description}
    }
}

请求

GET /recipes/_design/recipe/_show/description HTTP/1.1
Accept: application/json
Host: localhost:5984

响应

HTTP/1.1 200 OK
Content-Length: 6
Content-Type: text/html; charset=utf-8
Date: Wed, 21 Aug 2013 12:34:07 GMT
Etag: "7Z2TO7FPEMZ0F4GH0RJCRIOAU"
Server: CouchDB (Erlang/OTP)
Vary: Accept

no doc

1.5.8. /db/_design/design-doc/_show/show-name/doc-id

警告

Show函数在couchdb3.0中被弃用,并将在couchdb4.0中被删除。

GET /{db}/_design/{ddoc}/_show/{func}/{docid}
POST /{db}/_design/{ddoc}/_show/{func}/{docid}

应用 show function 对于指定的文档。

请求和响应参数取决于函数实现。

参数:
  • db -- 数据库名称
  • ddoc -- 设计文件名称
  • func -- 显示函数名
  • docid -- 文档ID
响应头:
  • ETag -- 响应签名
查询参数:
  • format (string) -- 返回响应的格式。使用人 provides() 功能
状态代码:

功能

function(doc, req) {
    if (!doc) {
        return {body: "no doc"}
    } else {
        return {body: doc.description}
    }
}

请求

GET /recipes/_design/recipe/_show/description/SpaghettiWithMeatballs HTTP/1.1
Accept: application/json
Host: localhost:5984

响应

HTTP/1.1 200 OK
Content-Length: 88
Content-Type: text/html; charset=utf-8
Date: Wed, 21 Aug 2013 12:38:08 GMT
Etag: "8IEBO8103EI98HDZL5Z4I1T0C"
Server: CouchDB (Erlang/OTP)
Vary: Accept

An Italian-American dish that usually consists of spaghetti, tomato sauce and meatballs.

1.5.9. /db/_design/design-doc/_list/list-name/view-name

警告

列表函数在couchdb3.0中被弃用,并将在couchdb4.0中被删除。

GET /{db}/_design/{ddoc}/_list/{func}/{view}
POST /{db}/_design/{ddoc}/_list/{func}/{view}

应用 list function 对于 view function 来自同一个设计文件。

请求和响应参数取决于函数实现。

参数:
  • db -- 数据库名称
  • ddoc -- 设计文件名称
  • func -- 列表函数名
  • view -- 视图函数名
响应头:
查询参数:
  • format (string) -- 返回响应的格式。使用人 provides() 功能
状态代码:

功能

function(head, req) {
    var row = getRow();
    if (!row){
        return 'no ingredients'
    }
    send(row.key);
    while(row=getRow()){
        send(', ' + row.key);
    }
}

请求

GET /recipes/_design/recipe/_list/ingredients/by_name HTTP/1.1
Accept: text/plain
Host: localhost:5984

响应

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Wed, 21 Aug 2013 12:49:15 GMT
Etag: "D52L2M1TKQYDD1Y8MEYJR8C84"
Server: CouchDB (Erlang/OTP)
Transfer-Encoding: chunked
Vary: Accept

meatballs, spaghetti, tomato sauce

1.5.10. /db/_design/design-doc/_list/list-name/other-ddoc/view-name

警告

列表函数在couchdb3.0中被弃用,并将在couchdb4.0中被删除。

GET /{db}/_design/{ddoc}/_list/{func}/{other-ddoc}/{view}
POST /{db}/_design/{ddoc}/_list/{func}/{other-ddoc}/{view}

应用 list function 对于 view function 从其他设计文件。

请求和响应参数取决于函数实现。

参数:
  • db -- 数据库名称
  • ddoc -- 设计文件名称
  • func -- 列表函数名
  • other-ddoc -- 保存视图功能的其他设计文档名称
  • view -- 视图函数名
响应头:
查询参数:
  • format (string) -- 返回响应的格式。使用人 provides() 功能
状态代码:

功能

function(head, req) {
    var row = getRow();
    if (!row){
        return 'no ingredients'
    }
    send(row.key);
    while(row=getRow()){
        send(', ' + row.key);
    }
}

请求

GET /recipes/_design/ingredient/_list/ingredients/recipe/by_ingredient?key="spaghetti" HTTP/1.1
Accept: text/plain
Host: localhost:5984

响应

HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Wed, 21 Aug 2013 12:49:15 GMT
Etag: "5L0975X493R0FB5Z3043POZHD"
Server: CouchDB (Erlang/OTP)
Transfer-Encoding: chunked
Vary: Accept

spaghetti

1.5.11. /db/_design/design-doc/_update/update-name

POST /{db}/_design/{ddoc}/_update/{func}

执行 update function 在服务器端 null 文件。

参数:
  • db -- 数据库名称
  • ddoc -- 设计文件名称
  • func -- 更新函数名
响应头:
  • X-Couch-Id -- 已创建/更新文档的ID
  • X-Couch-Update-Newrev -- 创建/更新文件的修订
状态代码:

功能

function(doc, req) {
    if (!doc){
      return [null, {'code': 400,
                     'json': {'error': 'missed',
                              'reason': 'no document to update'}}]
    } else {
        doc.ingredients.push(req.body);
        return [doc, {'json': {'status': 'ok'}}];
    }
}

请求

POST /recipes/_design/recipe/_update/ingredients HTTP/1.1
Accept: application/json
Content-Length: 10
Content-Type: application/json
Host: localhost:5984

"something"

响应

HTTP/1.1 404 Object Not Found
Cache-Control: must-revalidate
Content-Length: 52
Content-Type: application/json
Date: Wed, 21 Aug 2013 14:00:58 GMT
Server: CouchDB (Erlang/OTP)

{
    "error": "missed",
    "reason": "no document to update"
}

1.5.12. /db/_design/design-doc/_update/update-name/doc-id

PUT /{db}/_design/{ddoc}/_update/{func}/{docid}

执行 update function 在指定文档的服务器端。

参数:
  • db -- 数据库名称
  • ddoc -- 设计文件名称
  • func -- 更新函数名
  • docid -- 文档ID
响应头:
  • X-Couch-Id -- 已创建/更新文档的ID
  • X-Couch-Update-Newrev -- 创建/更新文件的修订
状态代码:

功能

function(doc, req) {
    if (!doc){
        return [null, {'code': 400,
                       'json': {'error': 'missed',
                                'reason': 'no document to update'}}]
    } else {
        doc.ingredients.push(req.body);
        return [doc, {'json': {'status': 'ok'}}];
    }
}

请求

POST /recipes/_design/recipe/_update/ingredients/SpaghettiWithMeatballs HTTP/1.1
Accept: application/json
Content-Length: 5
Content-Type: application/json
Host: localhost:5984

"love"

响应

HTTP/1.1 201 Created
Cache-Control: must-revalidate
Content-Length: 16
Content-Type: application/json
Date: Wed, 21 Aug 2013 14:11:34 GMT
Server: CouchDB (Erlang/OTP)
X-Couch-Id: SpaghettiWithMeatballs
X-Couch-Update-NewRev: 12-a5e099df5720988dae90c8b664496baf

{
    "status": "ok"
}