MongoDB完整性更新边缘案例 [英] MongoDB Integrity Update edge case

查看:57
本文介绍了MongoDB完整性更新边缘案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我在数据库中有以下JSON,

Hello I have the following JSON in database,

{
   recordName : String
   amount : Number
   approved : Boolean
}

让我们说有两个用户在相同的时间发出这两个命令

Lets say I have two users issue these two command at the same time

Record.update({recordName: "test", approved: false},{$set: {amount : 5000, approved: false,...)

Record.update({recordName: "test", approved: false},{$set: {amount : 9999, approved: true,...)

是否始终保证最终结果始终为9999并批准为真?

Does it always guarantee that the final results will always be amount 9999 and approve true?

我担心最终结果有时可能是5000,并且批准是错误的.

I am worried that the final result might sometimes be amount 5000 and approve is false.

我不确定MongoDB管道.

I am not really sure about MongoDB pipeline.

我相信更新分为两个阶段吗? Find {recordName: "test", approved: false}然后更新?如果两个查询都已经找到了条目,那将取决于谁先更新?

I believe update is splitted into two stages? Find {recordName: "test", approved: false} then update? What if both query already found the entry, then it all depends on who updates first?

推荐答案

因为单个文档更新为

Because single document updates are atomic, regardless of the order that your two commands execute, the document will end up as:

{amount : 9999, approved: true, ...}

如果第一个命令先执行,那么第二个命令将覆盖它.

If the first command executes first, then the second command will override it.

如果第二条命令首先执行,则第一条命令无效,因为approve现在为true,因此更新条件将不匹配.

If the second command executes first, then the first command has no effect as approve is now true so the update conditions won't match.

您正在(正确)执行的操作是完善的乐观并发如果当前更新" 管理并发访问的方法.

What you're (correctly) doing is the well-established optimistic concurrency or "update if current" approach to managing concurrent access.

这篇关于MongoDB完整性更新边缘案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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