【问题标题】:Document update conflict with CouchDB design update文档更新与 CouchDB 设计更新冲突
【发布时间】: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","re​​ason":"文档更新冲突。"}

我觉得我在理解 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


【解决方案1】:

根据 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"}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    • 2017-03-22
    • 2022-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多