# MongoServerError：IndexKeyTooLarge：命名空间 test.events 上复合索引 'idx_1_2_3' 的索引键大小为 1050 字节，超过了最大索引键大小 1024 字节

- **ID:** `mongodb/index-key-too-large-for-compound-index`
- **领域:** mongodb
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 78%

## 根因

文档中索引字段的组合大小超过了 MongoDB 的 1024 字节索引键限制，通常由于大字符串或数组值导致。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| mongodb-5.0 | active | — | — |
| mongodb-6.0 | active | — | — |
| mongodb-7.0 | active | — | — |
| mongodb-8.0 | active | — | — |

## 解决方案

1. ```
   Reduce the size of indexed fields by truncating long strings or using smaller data types (e.g., ObjectId instead of string IDs).
   ```
2. ```
   Use a partial index that only indexes documents with smaller field values to avoid the limit.
   ```
3. ```
   Split the compound index into separate indexes and use $or queries to combine them, though this may affect performance.
   ```

## 无效尝试

- **** — Increasing the index key limit is not possible; it is a hard limit in MongoDB. (50% 失败率)
- **** — Dropping the compound index and creating multiple single-field indexes may degrade query performance for multi-field queries. (50% 失败率)
- **** — Using a hashed index on all fields reduces query flexibility and does not support range queries. (50% 失败率)
