# MongoError：连接池耗尽

- **ID:** `data/mongodb-connection-pool-exhausted`
- **领域:** data
- **类别:** resource_error
- **错误码:** `MongoError`
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

MongoDB 驱动程序的连接池中的所有连接都已被占用，无法创建新连接，通常是由于查询缓慢或池大小不足导致。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| MongoDB 5.0 | active | — | — |
| MongoDB 6.0 | active | — | — |
| MongoDB 7.0 | active | — | — |
| Node.js driver 4.0+ | active | — | — |

## 解决方案

1. ```
   Increase maxPoolSize and add connection pooling monitoring to detect slow operations. In Node.js: `const client = new MongoClient(uri, { maxPoolSize: 100 });`
   ```
2. ```
   Optimize slow queries by adding indexes and using explain() to identify full collection scans. Example: `db.collection.createIndex({ field: 1 });`
   ```
3. ```
   Implement connection pool draining and retry logic in application code to gracefully handle transient exhaustion.
   ```

## 无效尝试

- **** — More connections increase contention and memory usage on the server, causing slower queries and more timeouts. (65% 失败率)
- **** — The root cause (slow queries or insufficient indexing) remains unaddressed. (40% 失败率)
- **** — Timeouts may kill slow queries but don't fix the underlying performance issue, leading to data inconsistency. (55% 失败率)
