mongodbcompound-index

Key length for Compound index


I'm looking to upgrade a MongoDB 2.4.x instance to 2.6. As part of this process I ran the db.upgradeCheckAllDBs() method to ensure my data is in a correct state for the upgrade. This check found a number of records in my database for which there was an index defined on a field, but the key exceeded the 1024 byte limit. That is, I saw errors of the form:

Document Error: key for index {  "v" : 1,  "name" : "field_1",  "key" : {  "field" : 1 },  "ns" : "mydb.users" } too long for document

Indicating that document contained a value in the field field which exceeded 1024 bytes (the limit for a key).

This is straightforward to resolve (I can just remove that index on field), but what about Compound Indexes. Ex: say I have an index like the following:

Document Error: key for index {  "v" : 1,  "name" : "email_-1_meta_-1",  "key" : {  "email" : -1,  "meta" : -1 },  "ns" : "mydb.users" } too long for document

Does this mean that the document had a length of the email and meta fields combined surpass 1024 bytes? Or is it 1024 bytes for each field, or some other combination? That is, for a compound index, how does it determine that the key length exceeds the 1024 byte limit?


Solution

  • The total size of an index entry, which can include structural overhead depending on the BSON type, must be less than 1024 bytes.

    If we have compound index on f1, f2, f3 -> fi+f2+f3+structural overhead < 1024

    more here