org.springframework.dao.DataIntegrityViolationException: Cannot create index for '' in collection 'userMobileVerificationRequestVO' with keys '{ "userId" : 1 , "mobile" : -1}' and options '{ "name" : "userId_mobile_idx" , "unique" : true}'.; nested exception is com.mongodb.CommandFailureException: { "serverUsed" : "ip-10-0-3-31:27017" , "ok" : 0.0 , "errmsg" : "Index with pattern: { userId: 1, mobile: -1 } already exists with different options" , "code" : 85}

Spring JIRA | sunny mittal | 6 months ago
  1. 0

    We were getting the exception {noformat} org.springframework.dao.DataIntegrityViolationException: Cannot create index for '' in collection 'userMobileVerificationRequestVO' with keys '{ "userId" : 1 , "mobile" : -1}' and options '{ "name" : "userId_mobile_idx" , "unique" : true}'.; nested exception is com.mongodb.CommandFailureException: { "serverUsed" : "ip-10-0-3-31:27017" , "ok" : 0.0 , "errmsg" : "Index with pattern: { userId: 1, mobile: -1 } already exists with different options" , "code" : 85} at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.createIndex(MongoPersistentEntityIndexCreator.java:157) at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForAndCreateIndexes(MongoPersistentEntityIndexCreator.java:133) at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForIndexes(MongoPersistentEntityIndexCreator.java:125) at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:109) {noformat} I went through the source code and found that we are checking if the index exists after we get exception. Why are we not doing this before creating an index? I think we should check if an index exists before creating an index, otherwise this would always throw exceptions whenever application/server is restarted as the classesSeen map would be empty initially. {code:java} void createIndex(IndexDefinitionHolder indexDefinition) { try { mongoDbFactory.getDb().getCollection(indexDefinition.getCollection()).createIndex(indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions()); } catch (MongoException ex) { if (MongoDbErrorCodes.isDataIntegrityViolationCode(ex.getCode())) { *DBObject existingIndex = fetchIndexInformation(indexDefinition);* String message = "Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'."; *if (existingIndex != null) {* message += " Index already defined as '%s'."; } throw new DataIntegrityViolationException( String.format(message, indexDefinition.getPath(), indexDefinition.getCollection(), indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions(), existingIndex), ex); } RuntimeException exceptionToThrow = mongoDbFactory.getExceptionTranslator().translateExceptionIfPossible(ex); throw exceptionToThrow != null ? exceptionToThrow : ex; } } {code}

    Spring JIRA | 6 months ago | sunny mittal
    org.springframework.dao.DataIntegrityViolationException: Cannot create index for '' in collection 'userMobileVerificationRequestVO' with keys '{ "userId" : 1 , "mobile" : -1}' and options '{ "name" : "userId_mobile_idx" , "unique" : true}'.; nested exception is com.mongodb.CommandFailureException: { "serverUsed" : "ip-10-0-3-31:27017" , "ok" : 0.0 , "errmsg" : "Index with pattern: { userId: 1, mobile: -1 } already exists with different options" , "code" : 85}
  2. 0

    We were getting the exception {noformat} org.springframework.dao.DataIntegrityViolationException: Cannot create index for '' in collection 'userMobileVerificationRequestVO' with keys '{ "userId" : 1 , "mobile" : -1}' and options '{ "name" : "userId_mobile_idx" , "unique" : true}'.; nested exception is com.mongodb.CommandFailureException: { "serverUsed" : "ip-10-0-3-31:27017" , "ok" : 0.0 , "errmsg" : "Index with pattern: { userId: 1, mobile: -1 } already exists with different options" , "code" : 85} at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.createIndex(MongoPersistentEntityIndexCreator.java:157) at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForAndCreateIndexes(MongoPersistentEntityIndexCreator.java:133) at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForIndexes(MongoPersistentEntityIndexCreator.java:125) at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:109) {noformat} I went through the source code and found that we are checking if the index exists after we get exception. Why are we not doing this before creating an index? I think we should check if an index exists before creating an index, otherwise this would always throw exceptions whenever application/server is restarted as the classesSeen map would be empty initially. {code:java} void createIndex(IndexDefinitionHolder indexDefinition) { try { mongoDbFactory.getDb().getCollection(indexDefinition.getCollection()).createIndex(indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions()); } catch (MongoException ex) { if (MongoDbErrorCodes.isDataIntegrityViolationCode(ex.getCode())) { *DBObject existingIndex = fetchIndexInformation(indexDefinition);* String message = "Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'."; *if (existingIndex != null) {* message += " Index already defined as '%s'."; } throw new DataIntegrityViolationException( String.format(message, indexDefinition.getPath(), indexDefinition.getCollection(), indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions(), existingIndex), ex); } RuntimeException exceptionToThrow = mongoDbFactory.getExceptionTranslator().translateExceptionIfPossible(ex); throw exceptionToThrow != null ? exceptionToThrow : ex; } } {code}

    Spring JIRA | 6 months ago | sunny mittal
    org.springframework.dao.DataIntegrityViolationException: Cannot create index for '' in collection 'userMobileVerificationRequestVO' with keys '{ "userId" : 1 , "mobile" : -1}' and options '{ "name" : "userId_mobile_idx" , "unique" : true}'.; nested exception is com.mongodb.CommandFailureException: { "serverUsed" : "ip-10-0-3-31:27017" , "ok" : 0.0 , "errmsg" : "Index with pattern: { userId: 1, mobile: -1 } already exists with different options" , "code" : 85}
  3. 0

    GitHub comment 52#151558461

    GitHub | 1 year ago | ghsnd
    org.springframework.dao.DataIntegrityViolationException: A different object with the same identifier value was already associated with the session : [eu.freme.common.persistence.model.Template#1]; nested exception is javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [eu.freme.common.persistence.model.Template#9]
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Spring Data Mongo - "Missing 'ns' field while inserting into XXX.system.indexes"

    Stack Overflow | 1 year ago | zdlgrj
    org.springframework.dao.DataIntegrityViolationException: { "serverUsed" : "localhost/127.0.0.1:27017" , "connectionId" : 1314 , "err" : "Missing \"ns\" field while inserting into XXX.system.indexes" , "code" : 4 , "n" : 0 , "ok" : 1.0}; nested exception is com.mongodb.WriteConcernException: { "serverUsed" : "localhost/127.0.0.1:27017" , "connectionId" : 1314 , "err" : "Missing \"ns\" field while inserting into Taxation.system.indexes" , "code" : 4 , "n" : 0 , "ok" : 1.0}
  6. 0

    Removing an entity and create new one with same primary key

    Stack Overflow | 11 months ago | Dmitry Shipaev
    org.springframework.dao.DataIntegrityViolationException: A different object with the same identifier value was already associated with the session : [com.test.core.model.subsubentity.SubSubEntity#3440120160113000103]; nested exception is javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [com.test.core.model.subsubentity.SubSubEntity#3440120160113000103]

    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.DataIntegrityViolationException

      Cannot create index for '' in collection 'userMobileVerificationRequestVO' with keys '{ "userId" : 1 , "mobile" : -1}' and options '{ "name" : "userId_mobile_idx" , "unique" : true}'.; nested exception is com.mongodb.CommandFailureException: { "serverUsed" : "ip-10-0-3-31:27017" , "ok" : 0.0 , "errmsg" : "Index with pattern: { userId: 1, mobile: -1 } already exists with different options" , "code" : 85}

      at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.createIndex()
    2. Spring Data MongoDB - Core
      MongoPersistentEntityIndexCreator.onApplicationEvent
      1. org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.createIndex(MongoPersistentEntityIndexCreator.java:157)
      2. org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForAndCreateIndexes(MongoPersistentEntityIndexCreator.java:133)
      3. org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForIndexes(MongoPersistentEntityIndexCreator.java:125)
      4. org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:109)
      4 frames