根据有关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我的工作流程如下:
问题在于,第一次添加新文档时,一切运行正常,但是下次发布相同文档时,每个文档都会出现以下错误:
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数据库而每个文档没有
发布评论