使用CouchDB以事务方式更新两个文档 [英] Transaction-like update of two documents using CouchDB

查看:85
本文介绍了使用CouchDB以事务方式更新两个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般来说,作为CouchDB或NoSQL的新手,我找不到更新两个文档的好方法,无法保证两个文档都被更新或都不被更新。

As a newbie to CouchDB or NoSQL in general I can't find a good way of updating two documents, with guarantee that either both are updated or none of them.

在我的用例中,每个文档中都有一个布尔标志。为了说明这一点,假设我正在谈论带有布尔属性isKing的类型为 citizen的文档。我想确保一次只有一位国王。当我想换个国王时,这变得很棘手。这需要修改两个文档(将新国王设置为isKing = true,将旧国王设置为isKing = false)。

In my use case there is a boolean flag in each document. To illustrate, lets assume I'm talking about document of type="citizen" with a boolean attribute isKing. I want to ensure there is exactly one king at a time. It gets tricky when I want to change the king. This requires modification of two documents (to set isKing=true for the new king and isKing=false to the old one).

如何确保我不会结束不幸的并发更新后,有两个或零个国王?

How do I ensure I won't end up with two or zero kings after some unfortunate concurrent updates?

我在考虑批量更新,但由于它不支持事务处理,因此没有帮助。

I was thinking about bulk update, but it doesn't help since it does not support transactions.

编辑:我见过问题我可以在CouchDB中进行事务和锁定吗?,但是它不能解决我的情况。它也与CouchDB中的事务有关,但这就是相似之处结束的地方。交易阅读与处理的问题更新一个文档,而我要询问两个文档的事务更新。我找不到其他问题的答案对我的情况有帮助,但是如果您认为重复,请解释原因。

I've seen question Can I do transactions and locks in CouchDB?, but it does not address my case. It also relates to transactions in CouchDB, but that's where similarities end. The problem there is to transactionally read & update one document, while I'm asking about transactional update of two documents. I don't find answers to the other question helpful for my case, but if you think it is duplicate please explain why.

推荐答案


具有批量更新的事务语义

总之,没有(按设计)。但是,您可以要求CouchDB检查_bulk_docs请求中的所有文档是否都通过了所有验证功能。如果甚至有一个失败,则不会写入任何文档。您可以通过在请求中包含 all_or_nothing:true来选择此模式。

In short, there are none (by design). However, you can ask CouchDB to check that all the documents in your _bulk_docs request pass all your validation functions. If even one fails, none of the documents are written. You can select this mode by including "all_or_nothing":true in your request.

在自己的 validate_doc_update中处理版本验证函数,通过比较 oldDoc._rev newDoc._rev

Handle the revision validation in an own validate_doc_update function by comparing oldDoc._rev and newDoc._rev.

如果您让一个文档的验证失败-其他文档也不会被写入。

If you let fail the validation of one doc - the other will also not be written.

注意!!!答案仅对CouchDB v1有效。 CouchDB v2忽略了 all_or_nothing属性。

这篇关于使用CouchDB以事务方式更新两个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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