【发布时间】:2019-05-23 20:38:48
【问题描述】:
我正在尝试创建一个等效于传统 RDBM 中使用的创建/更新触发器。 create_ts 的创建很好,但是 update_ts 部分对我不起作用。
"updates": {
"add_ts": "function(doc, req)
{ if(!doc){
var result=JSON.parse(req.body);
result.created_ts=new Date();
return [result, 'Created']
}
doc.update_ts=new Date();
return [doc,'Updated'];
}"
},
文件可以创建:
curl -X POST $COUCHDB_URL/mobile_gateway/_design/devicetokens/_update/add_ts -d ' {"_id":"aaaa", "boris":"Ioffe"} '
{
"_id": "aaaa",
"_rev": "7-70069ed48a5fa2a571b5ad83067010b9",
"boris": "Ioffe",
"created_ts": "2018-12-24T20:24:58.064Z"
}
curl -X PUT $COUCHDB_URL/mobile_gateway/_design/devicetokens/_update/add_ts -d ' {"_id":"aaaa", "boris":"Loffe"} '
{"error":"conflict","reason":"文档更新冲突。"}
我觉得我在理解 couchdb 文档更新时遗漏了一些基本的东西。
【问题讨论】:
-
更新文档时,还必须包含修订。否则它会尝试创建它,并发现它已经存在。
-
我想的也差不多。但是文档怎么没有提到这一点。 docs.couchdb.org/en/2.3.0/ddocs/ddocs.html#update-functions
-
When the request to an update handler includes a document ID in the URL, the server will provide the function with the most recent version of that document.根据这句话看来你在PUT的url中省略了id,只提供了body。 -
最后的评论应该提升回答!
标签: triggers couchdb couchdb-2.0 design-documents