我正在尝试从MyCouch调用更新函数。My沙发的文档只报告了一个过时的示例,其中使用了_design文档上的Post方法。但是如何使用更新函数
{
"_id": "_design/artists",
"language": "javascript",
"views": {
"albums": {
"map": "function(doc) { if(doc.$doctype !== 'artist') return; emit(doc.name, doc.albums);}"
}
}
};
client.Documents.Post(designDocumentAsJson);如何在推送新文档的_design couchDB上执行更新函数?
couchDB文档讲述了这个调用。
PUT /{db}/_design/{ddoc}/_update/{func}/{docid}发布于 2019-10-31 14:10:42
PUT /{db}/_design/{ddoc}/_update/{func}/{docid}
Executes update function on server side for the specified document.
Parameters:
db – Database name
ddoc – Design document name
func – Update function name
docid – Document ID可以像下面这样将设计文档插入到db:https://docs.couchdb.org/en/stable/api/ddoc/render.html#db-design-design-doc-update-update-name
{
"_id": "_design/albums",
"language": "javascript",
"updates": {
"addAlbum": "function(doc, req) {\n if (!doc){\n return [null, {'code': 400,\n 'json': {'error': 'missed',\n 'reason': 'no document to update'}}]\n } else {\n var body = JSON.parse(req.body);\n doc.albums.push(body.album);\n return [doc, {'json': {'status': 'ok'}}];\n }\n}\n"
}
}职能(没有串线):
function(doc, req) {
if (!doc){
return [null, {'code': 400,
'json': {'error': 'missed',
'reason': 'no document to update'}}]
} else {
var body = JSON.parse(req.body);
doc.albums.push(body.album);
return [doc, {'json': {'status': 'ok'}}];
}
}示例文档:
{
"_id": "albumsId1",
"albums": [
1
]
}后Api
请求:
POST http://localhost:5984/test/_design/albums/_update/addAlbum/albumsId1
Content-Type:application/json
Accept:application/json
{"album":2}响应:
{
"status": "ok"
}更新后的doc
{
"_id": "albumsId1",
"_rev": "19-7edb16db3bae388685f554138d562bd0",
"albums": [
1,
2
]
}https://stackoverflow.com/questions/58604711
复制相似问题