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?
Asked Answered
W

3

11
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)
Willetta answered 10/2, 2015 at 20:22 Comment(1)
or you could shorten the names... there's no real reason to use a full-length novel as the name of a key.Pasargadae
P
29

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"});
Phillada answered 18/10, 2015 at 19:2 Comment(1)
For those who experienced this issue with Mongoose (Node.js ODM for MongoDB), this parameter works as well ;-) ThxKenelm
L
0

record my case for other refer:

  • my case: code
        indexKeyList = [
            ("shortLink", pymongo.ASCENDING),
            ("parsedLink.isParseOk", pymongo.ASCENDING),
            ("parsedLink.errType", pymongo.ASCENDING),
            ("parsedGame.realGameName", pymongo.ASCENDING),
            ("parsedGame.gameTheme", pymongo.ASCENDING),
        ]
        mongoCollectionShortlink.create_index(indexKeyList)
  • report error:
发生异常: OperationFailure

namespace name generated from index name "shortLink.gameShortLink.$shortLink_1_parsedLink.isParseOk_1_parsedLink.errType_1_parsedGame.realGameName_1_parsedGame.gameTheme_1" is too long (127 byte max), full error: {'ok': 0.0, 'errmsg': 'namespace name generated from index name "shortLink.gameShortLink.$shortLink_1_parsedLink.isParseOk_1_parsedLink.errType_1_parsedGame.realGameName_1_parsedGame.gameTheme_1" is too long (127 byte max)', 'code': 67, 'codeName': 'CannotCreateIndex'}
  • root cause: use create_index to create multiple index. then multiple index name joined together, cause name too long, exceed 127 byte limit

  • Solution: should use create_indexes

import pymongo
from pymongo import IndexModel

        indexShortLink = IndexModel([("shortLink", pymongo.ASCENDING)], name="shortLink")
        indexIsParseOk = IndexModel([("parsedLink.isParseOk", pymongo.ASCENDING)], name="parsedLink_isParseOk")
        indexErrType = IndexModel([("parsedLink.errType", pymongo.ASCENDING)], name="parsedLink_errType")
        indexRealGameName = IndexModel([("parsedGame.realGameName", pymongo.ASCENDING)], name="parsedGame_realGameName")
        indexGameTheme = IndexModel([("parsedGame.gameTheme", pymongo.ASCENDING)], name="parsedGame_gameTheme")
        indexModelList = [
            indexShortLink,
            indexIsParseOk,
            indexErrType,
            indexRealGameName,
            indexGameTheme,
        ]
        mongoCollectionShortlink.create_indexes(indexModelList)
Litigation answered 19/8, 2021 at 2:14 Comment(0)
E
-1

You can not disable indexing as MongoDB will always create an index for _id. Shorten your collection name instead - saves you some typing too

Exculpate answered 10/2, 2015 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.