I'm creating a index in mongo:
db.table.createIndex({"chr" : 1, "Start" : 1, "End" : 1, "Ref" : 1, "Alt" : 1})
It runs for some time and gives an error msg:
error in monogdb "errmsg" : "WiredTigerIndex::insert: key too large to index, failing
How do I fix this error?
Syntax. The basic syntax of createIndex() method is as follows(). Here key is the name of the field on which you want to create index and 1 is for ascending order. To create index in descending order you need to use -1.
You cannot drop the default index on the _id field. Starting in MongoDB 4.2, you cannot specify db. collection. dropIndex("*") to drop all non- _id indexes.
The indexes are ordered by the value of the field specified in the index. So, MongoDB provides a createIndex() method to create one or more indexes on collections. Using this method we can create different types of indexes like text index, 2dsphere index, 2d index, etc.
Indexes support the efficient execution of queries in MongoDB. Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement.
In MongoDB, since 2.6, the total size of an index entry must be less than 1024 bytes. Documentation here
In other terms, at least one of your documents has a large value in one of the field you are trying to index.
It's not a good idea in general to index very large values like that because it creates a big index which is less efficient compared to a smaller one and it takes more space in RAM which could be put to better use on a MongoDB node.
You could use this : mongod --setParameter failIndexKeyTooLong=false
.
But it doesn't look like a good idea. If you have a large text to index, you should consider using the Full Text index or you could use a Hashed index.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With