[MongoDB] Index names can only be set up to a maximum of 127 bytes
MongoDB index names can only be set up to a maximum of 127 bytes.
According to MongoDB’s official documentation, index names can be up to 127 characters maximum, and 128 characters or more cannot be set.
Index Name LengthFully qualified index names, which includes the namespace and the dot separators (i.e.
. .$ ), cannot be longer than 128 characters.
When MongoDB index name is too long and causes error
> db.users.createIndex( { email: 1 } , { name: "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345" } )
{
"ok" : 0,
"errmsg" : "namespace name generated from index name \\"test.users.$01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345\\" is too long (127 byte max)",
"code" : 67
}
When MongoDB index name length is OK and index creation succeeds
If the index name is within 127 characters including db name, collection name, and . or $ characters, you can create the index normally.
> db.users.createIndex( { email: 1 } , { name: "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234" } )
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
When creating indexes on single fields, you don’t need to particularly worry about index names.
When creating compound indexes, field names tend to be long by default, so it’s better to be mindful of index names.
That’s all from the Gemba where I was curious about the maximum length of MongoDB index names and investigated.