Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does MongoDB's update atomicity apply to both query and modification?

Tags:

mongodb

MongoDB has support for atomic updates. I.e. I can be sure that when a document is updated no other update will overwrite my previous change. My question relates to the combination of 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 above example, I only want to push a new player into a collection of players, if the number of players equals 2. Given the above query and update statement, is it possible that two simultaneous updates both push a player onto the same document, because at the time of reading the document its players $size is 2? I.e. does the atomicity span across the query and update part of the update statement or not?

Edit More in-depth sequence of events:

Consider firing the same update twice (U1 and U2) at the same time. Is the following sequence of events possible or not?

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

like image 901
David Walschots Avatar asked Apr 06 '12 08:04

David Walschots


People also ask

What is atomic update in MongoDB?

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.

Is MongoDB Upsert Atomic?

Upsert is not atomic.

Is MongoDB Inc Atomic?

Yes, all write operations with MongoDB are atomic at the level of a single document.

What is $Set in MongoDB?

$set outputs documents that contain all existing fields from the input documents and newly added fields. The $set stage is an alias for $addFields . Both stages are equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.


2 Answers

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

According to the answer by Marc (who works at 10gen) the situation described by me cannot occur.

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

like image 170
David Walschots Avatar answered Sep 18 '22 22:09

David Walschots


Update: not sure of my knowledge anymore... See "The ABA Nuance". Please don't accept this answer (or my comment below) as it is probably not correct. Would love to be corrected.


Your explanation of atomic is incorrect (I can be sure that when a document is updated no other update will overwrite my previous change). Other updates can (and will) overwrite your change. But they won't do it in a way that would interfere with integrity of your query.

It is important to know that MongoDB updates are atomic on single document. So when a document matches your query, it is "locked" and ready for an update. Note that your update ($push) works inside the same document that was locked. When update is finished, lock is released.

I am not sure I understand "does the atomicity span across the query and update part of the update statement or not", but: atomic means that other queries can't mess with our query. Our query can change data that is "locked" by itself.

Disclaimer: I am not privy to internal mechanisms MongoDB uses to ensure this atomicity, so this description might be lacking from technical viewpoint (especially in connection to locking) - but it is valid conceptually. This is how it works from external viewpoint.

like image 37
johndodo Avatar answered Sep 19 '22 22:09

johndodo