文档更新与 CouchDB 设计更新冲突
Document update conflict with CouchDB design update
我正在尝试创建与传统 RDBM 中使用的 create/update 触发器等效的方法。 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":"Document update conflict."}
我觉得我在理解 couchdb 文档更新时遗漏了一些基本知识。
根据 OP 请求将评论移至答案。
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。
您的请求应如下所示
curl -X PUT $COUCHDB_URL/mobile_gateway/_design/devicetokens/_update/add_ts/aaaa -d ' {"_id":"aaaa", "boris":"Loffe"}
我正在尝试创建与传统 RDBM 中使用的 create/update 触发器等效的方法。 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":"Document update conflict."}
我觉得我在理解 couchdb 文档更新时遗漏了一些基本知识。
根据 OP 请求将评论移至答案。
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。
您的请求应如下所示
curl -X PUT $COUCHDB_URL/mobile_gateway/_design/devicetokens/_update/add_ts/aaaa -d ' {"_id":"aaaa", "boris":"Loffe"}