问题
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"?
回答1:
When writing on a single document, the operation is atomic.
In MongoDB, a write operation is atomic on the level of a single document, even if the operation modifies multiple embedded documents within a single document.
When writing on multiple documents that is not atomic.
Further, a multi document transaction can be simulated using a two phase commit, details here.
来源:https://stackoverflow.com/questions/40398631/how-atomic-are-mongoengines-operations