While saving a collection MongoDB is creating Index name which is too long and exceeds 127 bytes limit. How to solve this. can i disable indexing?

shashidhar kache picture shashidhar kache · Feb 10, 2015 · Viewed 7.7k times · Source
com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "createdCollectionAutomatically" : true , "numIndexesBefore" : 1 , "ok" : 0.0 , "errmsg" : "namespace name generated from index name \"NDS.ABCD_pre_import.$importabilityEvaluations.perNameResults.straightImportResults.resultPolContent_NOT_IN_CURRENT_USE.officialPolResultNameContentId\" is too long (127 byte max)" , "code" : 67}
    at com.mongodb.CommandResult.getException(CommandResult.java:76)
    at com.mongodb.CommandResult.throwOnError(CommandResult.java:131)
    at com.mongodb.DBCollectionImpl.createIndex(DBCollectionImpl.java:362)
    at com.mongodb.DBCollection.createIndex(DBCollection.java:563)
    at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.createIndex(MongoPersistentEntityIndexCreator.java:136)
    at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForAndCreateIndexes(MongoPersistentEntityIndexCreator.java:129)
    at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForIndexes(MongoPersistentEntityIndexCreator.java:121)
    at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:105)
    at org.springframework.data.mongodb.core.index.MongoMappingEventPublisher.publishEvent(MongoMappingEventPublisher.java:60)
    at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:306)
    at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:180)
    at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:140)
    at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:67)
    at org.springframework.data.mongodb.core.MongoTemplate.determineCollectionName(MongoTemplate.java:1881)
    at org.springframework.data.mongodb.core.MongoTemplate.determineEntityCollectionName(MongoTemplate.java:1868)
    at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:825)

Answer

dji picture dji · Oct 18, 2015

You can pass an index name as parameter to ensureIndex:

db.collection.ensureIndex({"birds.parrots.macaw.blue.id": 1}, {name:"myIndex1"});

db.collection.ensureIndex({"birds.parrots.macaw.blue.id": 1, "field2": 1}, {name:"myIndex1"});