MongoDB supports atomic updates. That is, I can be sure that when updating a document, no other update will overwrite my previous change. My question is related to a combination of a query and update statement and is best illustrated by the example shown below.
db.foo.update( { state : 1, players: { $size: 2 } } , { $push: { players : { new player document } } }, false , true );
In the example above, I only want to insert a new player into the players collection if the number of players is 2. Given the above request and update instruction, is it possible that two simultaneous updates click on the player the same document, because while reading the document its players are the size $ 2? That is, does atomicity apply to the part of the request and update of the update instruction or not?
Edit More detailed sequence of events:
Read the launch of the same update (U1 and U2) at the same time. Is the following sequence of events possible?
- U1 detects that document # 1 matches the expression update request part.
- U2 detects that document # 1 matches the request part of the update instruction.
- U1 pushes a new player into document # 1.
- U2 pushes a new player in document # 1.
The end result is that document # 1 contains one more player than expected because both U1 and U2 were under the impression that document # 1 contained only two players.
David walschots
source share