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

Duplicate key error on replace with upsert

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Critical - P2 Critical - P2
    • None
    • Affects Version/s: 3.0.0, 3.2.0, 3.2.1
    • Component/s: Write Ops
    • Labels:
      None
    • ALL
    • Hide

      // Unique index on the collection
      Document index = new Document("brickId", 1);
      index.append("date", 1);
      index.append("brickCategory", 1);
      index.append("mortarCategory", 1);
      collection.createIndex(index, new IndexOptions().unique(true));

      // Document locating the record to replace
      Document searchDoc = new Document("brickId", "569f6ffa8ddccfb6412d9dc2");
      searchDoc.append("date", new Date(1296432000000));
      searchDoc.append("brickCategory", "Red");
      searchDoc.append("mortarCategory", "Steel");
      return searchDoc;

      // Document being inserted (or replaced)
      Document doc = new Document();
      doc.append(brickId, "569f6ffa8ddccfb6412d9dc2");
      doc.append("brickCategory", "Red");
      doc.append("mortarCategory", "Steel");
      doc.append("amount, 100);
      doc.append("date", new Date());

      // Code used to do replace with upsert(true) call
      UpdateResult result = getCollection().replaceOne(searchDoc, doc, new UpdateOptions().upsert(true));

      // Error bubbling up from mongo
      com.mongodb.MongoWriteException: E11000 duplicate key error index: mydb.Costs.$brickId_1_date_1_brickCategory_1_mortarCategory_1 dup key: { : "569f6ffa8ddccfb6412d9dc2", : new Date(1296432000000), : "Red", : "Steel" }
      at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:523) ~[MongoCollectionImpl.class:na]
      at com.mongodb.MongoCollectionImpl.replaceOne(MongoCollectionImpl.java:344) ~[MongoCollectionImpl.class:na]

      Other steps to reproduce:
      http://stackoverflow.com/questions/29305405/mongodb-impossible-e11000-duplicate-key-error-dup-key-when-upserting

      Show
      // Unique index on the collection Document index = new Document("brickId", 1); index.append("date", 1); index.append("brickCategory", 1); index.append("mortarCategory", 1); collection.createIndex(index, new IndexOptions().unique(true)); // Document locating the record to replace Document searchDoc = new Document("brickId", "569f6ffa8ddccfb6412d9dc2"); searchDoc.append("date", new Date(1296432000000)); searchDoc.append("brickCategory", "Red"); searchDoc.append("mortarCategory", "Steel"); return searchDoc; // Document being inserted (or replaced) Document doc = new Document(); doc.append(brickId, "569f6ffa8ddccfb6412d9dc2"); doc.append("brickCategory", "Red"); doc.append("mortarCategory", "Steel"); doc.append("amount, 100); doc.append("date", new Date()); // Code used to do replace with upsert(true) call UpdateResult result = getCollection().replaceOne(searchDoc, doc, new UpdateOptions().upsert(true)); // Error bubbling up from mongo com.mongodb.MongoWriteException: E11000 duplicate key error index: mydb.Costs.$brickId_1_date_1_brickCategory_1_mortarCategory_1 dup key: { : "569f6ffa8ddccfb6412d9dc2", : new Date(1296432000000), : "Red", : "Steel" } at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:523) ~ [MongoCollectionImpl.class:na] at com.mongodb.MongoCollectionImpl.replaceOne(MongoCollectionImpl.java:344) ~ [MongoCollectionImpl.class:na] Other steps to reproduce: http://stackoverflow.com/questions/29305405/mongodb-impossible-e11000-duplicate-key-error-dup-key-when-upserting

      We're getting a duplicate key error exception in a situation where one should never be occurring. It occurs infrequently, but consistently.

      Our collection has a unique key defined on a combination of fields. We then performa `replaceOne()` with `upsert(true)`. Generally this works fine, but a tiny percentage of the writes result in an error being thrown by mongo stating:

      `com.mongodb.MongoWriteException: E11000 duplicate key error index`

      Our mongo instance is a single installation with no clustering and using the latest driver / server- 3.2.1.

      Our client is using the default connection pool and is sending a hundreds of requests per second to the database. That said, our requests are unique in each is writing a different record to the database each time. It's not the case where we'd be sending a record with the same complex key via multiple threads at the same time.

      It's worth noting that when this error occurs and an attempt made to re-upsert the exact same record record, it has succeeded every time.

      We're using the Java driver, but research has shown other people have run into this same error on StackOverflow:
      http://stackoverflow.com/questions/29305405/mongodb-impossible-e11000-duplicate-key-error-dup-key-when-upserting

            Assignee:
            Unassigned Unassigned
            Reporter:
            TheAndruu Andrew Cuga [X]
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: