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 | 9 months ago
tip
Your exception is missing from the Samebug knowledge base.
Here are the best solutions we found on the Internet.
Click on the to mark the helpful solution and get rewards for you help.
  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 | 9 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 | 9 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}

    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