批量更新CouchDB数据库而每个文档没有

编程入门 行业动态 更新时间:2024-10-27 09:47:01
本文介绍了批量更新CouchDB数据库而每个文档没有_rev值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

根据有关PUT操作的CouchDB Wiki 。

要更新现有文档,您还发出PUT请求。在这种情况下,JSON正文必须包含_rev属性,该属性使CouchDB知道编辑基于的修订。如果当前存储在数据库中的文档版本不匹配,则返回409冲突错误。

To update an existing document, you also issue a PUT request. In this case, the JSON body must contain a _rev property, which lets CouchDB know which revision the edits are based on. If the revision of the document currently stored in the database doesn't match, then a 409 conflict error is returned.

我的目标是执行 bulk_docs更新:

curl -X POST [domain]/[couch db name]/_bulk_docs -H "Content-type: application/json" -d @[some document].json

我的工作流程如下:

  • 我的数据在Google文档电子表格中。
  • 我通过复制并粘贴到 先生。数据转换器
  • 我使用cURL(如上所示)添加/更新文档
  • My data is in an Google Docs spreadsheet.
  • I convert the spreadsheet data to JSON by copying and pasting into Mr. Data Converter
  • I use cURL (as show above) to add/update documents
  • 问题在于,第一次添加新文档时,一切运行正常,但是下次发布相同文档时,每个文档都会出现以下错误:

    The problem is that the first time I add new documents, everything works perfectly, however the next time I post the same documents, I get following error for each document:

    ... { id: 28,错误:冲突,原因:文档更新冲突。} ...

    ...{"id":"28"," error":"conflict","reason":"Document update conflict."}...

    是否可以在不包含 _rev 属性的情况下更新现有文档?

    Is there any way to update an existing document without including a _rev property?

    推荐答案

    通过设计,您不能盲目更新CouchDB文档,只能尝试更新文档的特定修订版。

    By design, you cannot update a CouchDB document blindly, you can only attempt to update a specific revision of a document.

    对于单个文档,您可以使用 CouchDB更新处理程序将其隐藏来自客户端的更新处理程序将被传递给现有文档(如果存在),包括其修订版。

    For a single document, you can use a CouchDB update handler to hide this from the client as an update handler will be passed the existing document (if it exists) including its revision.

    对于文档集合,使用 _bulk_docs 时,可以添加 new_edits:false 这将强制插入冲突而不是拒绝(尽管您仍然需要传递 _rev ,但不必是当前的)。

    For a collection of documents, when using _bulk_docs, you can add "new_edits": false which will forcibly insert conflicts instead of rejection (though you'll still need to pass a _rev, it just doesn't have to be the current one).

    总而言之,最好遵循规则。抓取您要更新的文档的当前版本,尝试进行更新,如果得到409,则获取新版本,视情况进行合并,然后再次更新。

    All that said, it would be better to follow the rules. Grab the current revision of the document you would like to update, attempt to update it, if you get a 409, get the new version, merge as appropriate, and update again.

    更多推荐

    批量更新CouchDB数据库而每个文档没有

    本文发布于:2023-10-30 03:53:07,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1541674.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:批量   文档   数据库   CouchDB

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!