是否有等效于@dynamodbversionattribute和有条件的书面写作?

发布于 2025-02-06 14:04:38 字数 98 浏览 0 评论 0 原文

我对MongoDB是相对较新的,我担心并发请求可以有效地突变单个文档,从而导致数据丢失(第一个写作将被第二篇写作clobberberberberberber)。 DynamoDB通过

I am relatively new to MongoDB and I am concerned that concurrent request could mutate a single document effectively causing data loss (the first write would be clobbered by the second write). DynamoDB handles this via Optimistic locking and conditional writes. Is there an equivalent in MongoDB? If not, how do you prevent concurrent request from clobbering one another?

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

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

发布评论

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

评论(2

夜光 2025-02-13 14:04:38

@dynamodbversionAttribute 来自Java SDK,该Java SDK将Java驱动程序包装在客户端。这不是DynamoDB作为数据库引擎提供的。来自

每当将此属性的记录写入数据库时​​,它将被递增,并在请求中添加条件以检查旧版本的精确匹配。

类似的功能在文档级别以词汇级别实现: https://thecodebarbarian.com/whats-new-inew-in-mongoose-5-10-optimistic-concurrency.html#optimistic-concorrencion-concurrency-version-versioning versioning

本质上只是单调的版本编号,作为过滤器添加到更新中。文档更新是原子和二进制的 - 要么发生或不发生。将 {__ V:12} 添加到过滤器中, {__ v:{$ inc:1} 进行更新,并检查文档的数量更新:1 =成功,0 =其他客户已经更新了文档,没有什么匹配的 {__ V:12} 条件。

@DynamoDBVersionAttribute comes from the Java SDK, which wraps Java driver on the client side. It's not what dynamodb provides as a database engine. From https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/enhanced/dynamodb/extensions/annotations/DynamoDbVersionAttribute.html :

Every time a record with this attribute is written to the database it will be incremented and a condition added to the request to check for an exact match of the old version.

Similar functionality is implemented on document level in mongoose: https://thecodebarbarian.com/whats-new-in-mongoose-5-10-optimistic-concurrency.html#optimistic-concurrency-versus-versioning

Essentially it's just monotonically incremented version number, added as a filter to the update. Document updates are atomic and binary - it either happened or not. Add the {__v:12} to the filter, and {__v:{$inc:1} to update, and check number of documents updated: 1 = success, 0 = the other client already updated the document and there is nothing matching {__v:12} condition.

你是年少的欢喜 2025-02-13 14:04:38

mongodb常见问题:并发

mongoDB允许多个客户端读取和写相同的数据。到
确保一致性,MongoDB使用锁定和并发控制
防止客户同时修改相同的数据。写信给
单个文档要么完全或根本不出现,客户
始终查看一致的数据。


vermongo:与mongodb 简单的文档版本

Vermongo是一种简单的版本,用于保持较旧MongoDB文件。

它的设计非常简单,并且对可以应用的文档类型没有任何约束。

Vermongo的乐观并发控制也可以用来仅检测和避免矛盾的更新,即使人们不想将较旧的修订保留在数据库中。


文档版本如何工作

mongoDB不提供直接的并发控制。常见
支持并发的模式是使用文档版本编号
(int)用作更新语句的过滤器

MongoDB FAQ: Concurrency

MongoDB allows multiple clients to read and write the same data. To
ensure consistency, MongoDB uses locking and concurrency control to
prevent clients from modifying the same data simultaneously. Writes to
a single document occur either in full or not at all, and clients
always see consistent data.


Vermongo: Simple Document Versioning with MongoDB

Vermongo is a simple versioning scheme for keeping older revision of MongoDB documents.

It is designed to be extremely simple and place no constraints on the type of documents that it can be applied to.

Vermongo's optimistic concurrency control can also be used to just detect and avoid conflicting updates, even when one does not want to keep older revisions in the database.


How document versioning works

MongoDB provides no out-of-the-box concurrency control. A common
pattern for supporting concurrency is using a document version number
(int) that is used as a filter for update statements

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