批量更新CouchDB数据库而每个文档没有_rev值? [英] Bulk updating a CouchDB database without a _rev value per document?

查看:84
本文介绍了批量更新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

我的工作流程如下:


  1. 我的数据在Google文档电子表格中。

  2. 我通过复制并粘贴到 先生。数据转换器

  3. 我使用cURL(如上所示)添加/更新文档

  1. My data is in an Google Docs spreadsheet.
  2. I convert the spreadsheet data to JSON by copying and pasting into Mr. Data Converter
  3. 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数据库而每个文档没有_rev值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆