批量更新 CouchDB 数据库,每个文档没有 _rev 值?

发布于 2024-11-29 00:13:31 字数 809 浏览 1 评论 0原文

根据CouchDB Wiki 关于 PUT 操作

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

我的目标是执行批量文档更新

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

我的工作流程如下:

  1. 我的数据位于 Google 文档电子表格中。
  2. 我通过将电子表格数据复制并粘贴到 Mr.数据转换器
  3. 我使用cURL(如上所示)添加/更新文档

问题是,第一次添加新文档时,一切正常,但是下次我发布相同的文档时,我每个文档都会出现以下错误:

...{"id":"28","error":"冲突","re​​ason":"文档更新冲突。"}...

有没有办法在不包含 _rev 的情况下更新现有文档 财产?

According to the CouchDB Wiki on PUT operations.

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.

My goal is to perform a bulk_docs update:

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

My workflow is like this:

  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"," error":"conflict","reason":"Document update conflict."}...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抹茶夏天i‖ 2024-12-06 00:13:31

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

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

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

话虽如此,遵守规则会更好。获取您想要更新的文档的当前版本,尝试更新它,如果收到 409,则获取新版本,适当合并,然后再次更新。

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

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.

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).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文