org.springframework.dao.DuplicateKeyException: { "serverUsed" : “[…]” , "ok" : 1 , "n" : 0 , "err" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: […].$_id_ dup key: { : ObjectId('[…]') }" , "code" : 11000}; nested exception is com.mongodb.MongoException$DuplicateKey: { "serverUsed" : "[…]" , "ok" : 1 , "n" : 0 , "err" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: […].$_id_ dup key: { : ObjectId('[…]') }" , "code" : 11000}

Spring JIRA | Nathan Cantelmo | 2 years ago
  1. 0

    If you have an existing @Document entity with a unique index and you later add an @Version field to the entity class, updates to that entity via MongoRepository.save() will generate a DuplicateKeyException. This appears to happen because MongoTemplate.doSaveVersioned() assumes that entities containing a null version field (rather than a null ID), do not currently exist and performs insert, rather than update in that case. From MongoTemplate.java: private <T> void doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity, String collectionName) { [...] // Fresh instance -> initialize version property if (version == null) { doInsert(collectionName, objectToSave, this.mongoConverter); } else { [...] Update update = Update.fromDBObject(dbObject, ID_FIELD); doUpdate(collectionName, query, update, objectToSave.getClass(), false, false); } Ideally, this behavior would be modified to check whether an id field already exists, and if so perform an update with the version field initialized to zero if null. For now, however, it’s relatively easy to work around it by manually creating the missing version fields on existing entities directly in Mongo. A partial stacktrace of the DuplicateKeyException being generated as described is included below. Note that we were testing against an older version of spring-data, but this is an issue on the current version as well. org.springframework.dao.DuplicateKeyException: { "serverUsed" : “[…]” , "ok" : 1 , "n" : 0 , "err" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: […].$_id_ dup key: { : ObjectId('[…]') }" , "code" : 11000}; nested exception is com.mongodb.MongoException$DuplicateKey: { "serverUsed" : "[…]" , "ok" : 1 , "n" : 0 , "err" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: […].$_id_ dup key: { : ObjectId('[…]') }" , "code" : 11000} at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:55) ~[spring-data-mongodb-1.5.2.RELEASE.jar!/:na] at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:1918) ~[spring-data-mongodb-1.5.2.RELEASE.jar!/:na] at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:412) ~[spring-data-mongodb-1.5.2.RELEASE.jar!/:na] at org.springframework.data.mongodb.core.MongoTemplate.insertDBObject(MongoTemplate.java:895) ~[spring-data-mongodb-1.5.2.RELEASE.jar!/:na] at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:717) ~[spring-data-mongodb-1.5.2.RELEASE.jar!/:na] at org.springframework.data.mongodb.core.MongoTemplate.doSaveVersioned(MongoTemplate.java:850) ~[spring-data-mongodb-1.5.2.RELEASE.jar!/:na] at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:837) ~[spring-data-mongodb-1.5.2.RELEASE.jar!/:na] at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:72) ~[spring-data-mongodb-1.5.2.RELEASE.jar!/:na] […]

    Spring JIRA | 2 years ago | Nathan Cantelmo
    org.springframework.dao.DuplicateKeyException: { "serverUsed" : “[…]” , "ok" : 1 , "n" : 0 , "err" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: […].$_id_ dup key: { : ObjectId('[…]') }" , "code" : 11000}; nested exception is com.mongodb.MongoException$DuplicateKey: { "serverUsed" : "[…]" , "ok" : 1 , "n" : 0 , "err" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: […].$_id_ dup key: { : ObjectId('[…]') }" , "code" : 11000}
  2. 0

    If you have an existing @Document entity with a unique index and you later add an @Version field to the entity class, updates to that entity via MongoRepository.save() will generate a DuplicateKeyException. This appears to happen because MongoTemplate.doSaveVersioned() assumes that entities containing a null version field (rather than a null ID), do not currently exist and performs insert, rather than update in that case. From MongoTemplate.java: private <T> void doSaveVersioned(T objectToSave, MongoPersistentEntity<?> entity, String collectionName) { [...] // Fresh instance -> initialize version property if (version == null) { doInsert(collectionName, objectToSave, this.mongoConverter); } else { [...] Update update = Update.fromDBObject(dbObject, ID_FIELD); doUpdate(collectionName, query, update, objectToSave.getClass(), false, false); } Ideally, this behavior would be modified to check whether an id field already exists, and if so perform an update with the version field initialized to zero if null. For now, however, it’s relatively easy to work around it by manually creating the missing version fields on existing entities directly in Mongo. A partial stacktrace of the DuplicateKeyException being generated as described is included below. Note that we were testing against an older version of spring-data, but this is an issue on the current version as well. org.springframework.dao.DuplicateKeyException: { "serverUsed" : “[…]” , "ok" : 1 , "n" : 0 , "err" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: […].$_id_ dup key: { : ObjectId('[…]') }" , "code" : 11000}; nested exception is com.mongodb.MongoException$DuplicateKey: { "serverUsed" : "[…]" , "ok" : 1 , "n" : 0 , "err" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: […].$_id_ dup key: { : ObjectId('[…]') }" , "code" : 11000} at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:55) ~[spring-data-mongodb-1.5.2.RELEASE.jar!/:na] at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:1918) ~[spring-data-mongodb-1.5.2.RELEASE.jar!/:na] at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:412) ~[spring-data-mongodb-1.5.2.RELEASE.jar!/:na] at org.springframework.data.mongodb.core.MongoTemplate.insertDBObject(MongoTemplate.java:895) ~[spring-data-mongodb-1.5.2.RELEASE.jar!/:na] at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:717) ~[spring-data-mongodb-1.5.2.RELEASE.jar!/:na] at org.springframework.data.mongodb.core.MongoTemplate.doSaveVersioned(MongoTemplate.java:850) ~[spring-data-mongodb-1.5.2.RELEASE.jar!/:na] at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:837) ~[spring-data-mongodb-1.5.2.RELEASE.jar!/:na] at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:72) ~[spring-data-mongodb-1.5.2.RELEASE.jar!/:na] […]

    Spring JIRA | 2 years ago | Nathan Cantelmo
    org.springframework.dao.DuplicateKeyException: { "serverUsed" : “[…]” , "ok" : 1 , "n" : 0 , "err" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: […].$_id_ dup key: { : ObjectId('[…]') }" , "code" : 11000}; nested exception is com.mongodb.MongoException$DuplicateKey: { "serverUsed" : "[…]" , "ok" : 1 , "n" : 0 , "err" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: […].$_id_ dup key: { : ObjectId('[…]') }" , "code" : 11000}
  3. 0

    DuplicateKeyException on sparse unique CompoundIndex in subcollection being empty list

    GitHub | 2 years ago | tkruse
    org.springframework.dao.DuplicateKeyException: { "serverUsed" : "localhost:27017" , "ok" : 1.0 , "err" : "E11000 duplicate key error index: foo_idx dup key : {[[5501b92f44aeab6043550e9b]] }" , "code" : 11001}; nested exception is com.mongodb.MongoException$DuplicateKey: { "serverUsed" : "localhost:27017" , "ok" : 1.0 , "err" : "E11000 duplicate key error index: foo_idx dup key : {[[5501b92f44aeab6043550e9b]] }" , "code" : 11001}
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Mongodb upsert throwing DuplicateKeyException

    Stack Overflow | 4 years ago | biliboc
    org.springframework.dao.DuplicateKeyException: E11000 duplicate key error index: sytrue.ngram.$_id_ dup key: { : "page two" }; nested exception is com.mongodb.MongoException$DuplicateKey: E11000 duplicate key error index: sytrue.ngram.$_id_ dup key: { : "page two" }
  6. 0

    Mongodb upsert operation seems not atomic which throws DuplicateKeyException

    Stack Overflow | 1 year ago | Jag
    org.springframework.dao.DuplicateKeyException: Write failed with error code 11000 and error message 'E11000 duplicate key error collection: msgdb.roster index: _id_ dup key: { : "sn88332138" }'; nested exception is com.mongodb.DuplicateKeyException: Write failed with error code 11000 and error message 'E11000 duplicate key error collection: msgdb.roster index: _id_ dup key: { : "sn88332138" }'

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. org.springframework.dao.DuplicateKeyException

      { "serverUsed" : “[…]” , "ok" : 1 , "n" : 0 , "err" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: […].$_id_ dup key: { : ObjectId('[…]') }" , "code" : 11000}; nested exception is com.mongodb.MongoException$DuplicateKey: { "serverUsed" : "[…]" , "ok" : 1 , "n" : 0 , "err" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: […].$_id_ dup key: { : ObjectId('[…]') }" , "code" : 11000}

      at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible()
    2. Spring Data MongoDB - Core
      SimpleMongoRepository.save
      1. org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:55)[spring-data-mongodb-1.5.2.RELEASE.jar!/:na]
      2. org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:1918)[spring-data-mongodb-1.5.2.RELEASE.jar!/:na]
      3. org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:412)[spring-data-mongodb-1.5.2.RELEASE.jar!/:na]
      4. org.springframework.data.mongodb.core.MongoTemplate.insertDBObject(MongoTemplate.java:895)[spring-data-mongodb-1.5.2.RELEASE.jar!/:na]
      5. org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:717)[spring-data-mongodb-1.5.2.RELEASE.jar!/:na]
      6. org.springframework.data.mongodb.core.MongoTemplate.doSaveVersioned(MongoTemplate.java:850)[spring-data-mongodb-1.5.2.RELEASE.jar!/:na]
      7. org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:837)[spring-data-mongodb-1.5.2.RELEASE.jar!/:na]
      8. org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:72)[spring-data-mongodb-1.5.2.RELEASE.jar!/:na]
      8 frames