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)
问问题
9741 次
3 回答
26
您可以将索引名称作为参数传递给 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"});
于 2015-10-18T19:02:30.927 回答
0
记录我的情况以供其他参考:
- 我的情况:代码
indexKeyList = [
("shortLink", pymongo.ASCENDING),
("parsedLink.isParseOk", pymongo.ASCENDING),
("parsedLink.errType", pymongo.ASCENDING),
("parsedGame.realGameName", pymongo.ASCENDING),
("parsedGame.gameTheme", pymongo.ASCENDING),
]
mongoCollectionShortlink.create_index(indexKeyList)
- 报告错误:
发生异常: 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'}
根本原因:使用create_index创建多个索引。然后多个索引名称连接在一起,导致名称太长,超过 127 字节限制
解决方案:应该使用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)
于 2021-08-19T02:14:17.027 回答
-1
您不能禁用索引,因为 MongoDB 将始终为_id
. 改为缩短您的收藏名称 - 也可以为您节省一些输入
于 2015-02-10T22:09:57.613 回答