MongoDB Index Limits
Additional overhead
Each index occupies a certain amount of storage space, and index operations are also required when performing insert, update, and delete operations. Therefore, if you rarely read from a collection, it is recommended not to use indexes.
Memory (RAM) usage
Since indexes are stored in memory (RAM), you should ensure that the size of the index does not exceed the memory limit.
If the size of the index is larger than the memory limit, MongoDB will delete some indexes, which will lead to performance degradation.
Query restrictions
Indexes cannot be used by the following queries:
- Regular expressions and non-operators, such as $nin, $not, etc.
- Arithmetic operators, such as $mod, etc.
- $where clause
Therefore, it is a good habit to check whether your statements use indexes; you can use explain to view.
Index key restrictions
Starting from version 2.6, if the value of an existing index field exceeds the index key limit, MongoDB will not create an index.
Inserting documents exceeding the index key limit
If a document's index field value exceeds the index key limit, MongoDB will not convert any document into an indexed collection. Similar to the mongorestore and mongoimport tools.
Maximum limits
- A collection cannot have more than 64 indexes
- The length of an index name cannot exceed 128 characters
- A compound index can have at most 31 fields