Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-20380

Upsert is not atomic when inserting

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.0.6
    • Component/s: Concurrency, WiredTiger
    • None
    • ALL
    • Hide

      Create a file named upsert.js with the following content:

      upsert.js
      var i;
      for(i=0; i<10; ++i) {
        db.upsert.update({a:1}, {$inc:{b:1}}, true);
      }
      

      On a Mongo 3.0 WiredTiger setup, run these commands in bash:

      mongo --eval "db.upsert.drop()";
      for((i=0;i<100;i++)); do mongo  upsert.js & done;
      mongo --eval "db.upsert.find().forEach(function(d){printjson(d);})";
      

      This produces something like this:

      { "_id" : ObjectId("55f3359949f88a6cd8a11d7a"), "a" : 1, "b" : 997 }
      { "_id" : ObjectId("55f3359949f88a6cd8a11d7b"), "a" : 1, "b" : 1 }
      { "_id" : ObjectId("55f3359949f88a6cd8a11d7c"), "a" : 1, "b" : 1 }
      { "_id" : ObjectId("55f3359949f88a6cd8a11d7d"), "a" : 1, "b" : 1 }
      

      It should produce something like this:

      { "_id" : ObjectId("55f3359949f88a6cd8a11d7a"), "a" : 1, "b" : 1000 }
      
      Show
      Create a file named upsert.js with the following content: upsert.js var i; for (i=0; i<10; ++i) { db.upsert.update({a:1}, {$inc:{b:1}}, true ); } On a Mongo 3.0 WiredTiger setup, run these commands in bash: mongo --eval "db.upsert.drop()"; for((i=0;i<100;i++)); do mongo upsert.js & done; mongo --eval "db.upsert.find().forEach(function(d){printjson(d);})"; This produces something like this: { "_id" : ObjectId("55f3359949f88a6cd8a11d7a"), "a" : 1, "b" : 997 } { "_id" : ObjectId("55f3359949f88a6cd8a11d7b"), "a" : 1, "b" : 1 } { "_id" : ObjectId("55f3359949f88a6cd8a11d7c"), "a" : 1, "b" : 1 } { "_id" : ObjectId("55f3359949f88a6cd8a11d7d"), "a" : 1, "b" : 1 } It should produce something like this: { "_id" : ObjectId("55f3359949f88a6cd8a11d7a"), "a" : 1, "b" : 1000 }

      When many upserts happen at the same time, and the queried document does not exist, there should be only one insert, and the rest of the upserts should result in updates to the inserted document.

      In Mongo 3.0 with the WiredTiger engine, more than one document gets inserted in this situation. This does not happen on MMAPv1, or on Mongo 2.6.

            Assignee:
            ramon.fernandez@mongodb.com Ramon Fernandez Marina
            Reporter:
            danielgalinkin@gmail.com Daniel Galinkin
            Votes:
            1 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: