Level of isolation of update

365 views
Skip to the first unread message

David Walschots

unread,
10 Apr 2012, 1:57:14 pm10/04/12
to mongodb-user
Consider two concurrently updates (U1, U2), structured as follows:

db.foo.update(
{ state : 1, players: { $size: 2 } } ,
{ $push: { players : { new player document } }, $set { state : 2 } },
false , true );

I don't want these updates to interfere with each other at the
document level. I.e. if U1 has updated document #1, then document #1
no longer matches the query part of update U2 and therefore should not
be modified.

My question is if the following (unwanted) sequence is possible, and
if so how this situation can be mitigated?

U1 finds that document #1 matches the query portion of the update
statement.
U2 finds that document #1 matches the query portion of the update
statement.
U1 pushes a new player in 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 contains only two players.

Thank you for your help.

Kind regards,

David Walschots

Marc

unread,
10 Apr 2012, 2:40:00 pm10/04/12
to mongodb-user
The situation that you described is not possible; there is no danger
of both updates modifying the same document.

For more information, please see the "Atomic Operations"
documentation:
http://www.mongodb.org/display/DOCS/Atomic+Operations
Reply all
Reply to author
Forward
0 new messages