Does MongoDB atomicity alter both the request and the modification?

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.

+7
source share
3 answers

I asked this question in the mongodb user group. http://groups.google.com/group/mongodb-user/browse_thread/thread/e61e220dc0f6f64c

In accordance with Mark's answer (which works in 10gen), the situation I described cannot happen.

The situation you described is impossible; there is no danger of both updates modifying the same document.

+3
source

Update: I don't know my knowledge anymore ... See "The ABA Nuance" . Please do not accept this answer (or my comment below) as it is probably incorrect. I would like to fix it.


Your explanation of the atom is incorrect (I can be sure that when updating a document, no other update will overwrite my previous change). Other updates may (and will) overwrite your changes. But they will not do this in a way that interferes with the integrity of your request.

It is important to know that MongoDB atomic updates in one document . Therefore, when the document matches your request, it is "blocked" and ready for updating. Please note that your update ( $push ) works inside the same document that was blocked. When the update is completed, the lock is released.

I'm not sure I understand, “atomicity extends to part of the request and update update instructions or not,” but: atomic means that other requests cannot ruin our request. Our request may change data that is "blocked" by itself.

Disclaimer: I am not tied to the internal mechanisms that MongoDB uses to ensure this atomicity, so this description may not be available from a technical point of view (especially in connection with locking), but it is really conceptual. This is how it works from an external point of view.

0
source

With the sequence of events that you record, you can really have one player too many. Updating “find” and “updating” works very much like doing a “search” and then “updating” for each of the documents that you repeat. You probably want to look at the $ atomic statement: http://www.mongodb.org/display/DOCS/Atomic+Operations#AtomicOperations-ApplyingtoMultipleObjectsAtOnce

0
source

All Articles