mongoengine's 操作的原子性如何 [英] How atomic are mongoengine's operations

查看:55
本文介绍了mongoengine's 操作的原子性如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试像这样在对象的状态之间转换:

I'm trying to transition between object's state like so:

User.objects(id=user_id, state=STATE_WAITING).update_one(set__state=STATE_FINISHED)

问题是,是否存在两个进程异步执行相同操作并成功的情况?

The question is, can there be a situation where two processes will make the same operation asynchronously and succeed?

如果是这样,这个操作是否可以是原子的,所以下一个尝试更新用户(同时异步)的进程会失败,因为它的状态已经完成"了?

If so, can this operation be atomic, so the next process that will try to update the user (at the same time asynchronously), will fail because it's status will be already "finished"?

推荐答案

在单个文档上写入时,操作是原子的.

When writing on a single document, the operation is atomic.

在MongoDB中,写操作是原子的在单一层面文档,即使操作修改了多个嵌入的文档在单个文档中.

在多个非原子文档上书写时.

When writing on multiple documents that is not atomic.

此外,可以使用两阶段提交来模拟多文档事务,详情此处.

Further, a multi document transaction can be simulated using a two phase commit, details here.

这篇关于mongoengine's 操作的原子性如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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