# MongoServerError: 事务对于快照太旧：读取时间戳早于存储引擎快照历史中的最旧时间戳

- **ID:** `mongodb/transaction-too-old-for-snapshot`
- **领域:** mongodb
- **类别:** resource_error
- **错误码:** `262`
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

多文档事务使用的读取时间戳已从 WiredTiger 存储引擎的快照历史中清除，通常是由于长时间运行的事务或快照保留不足。

## 版本兼容性

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

## 解决方案

1. ```
   Reduce the duration of transactions by committing them faster. Break large transactions into smaller batches. Example: instead of one transaction updating 10,000 documents, use 100 transactions of 100 documents each.
   ```
2. ```
   Increase the WiredTiger snapshot retention period by adjusting the `storage.wiredTiger.engineConfig.journalCompressor` or setting `eviction_dirty_target` to a higher value (requires server restart). Example: `db.adminCommand({setParameter:1,wiredTigerEngineConfig:{eviction_dirty_target:20}})`
   ```
3. ```
   If using secondary reads, ensure the secondary's oplog window is large enough to support the transaction's read timestamp. Monitor with `rs.printSlaveReplicationInfo()` and increase oplog size if needed.
   ```

## 无效尝试

- **** — This does not affect snapshot retention; the issue is the storage engine pruning old snapshots, not the transaction timing out. (20% 失败率)
- **** — Restarting resets the snapshot history but does not prevent pruning; the error will recur if the same pattern occurs. (10% 失败率)
- **** — Changing read concern may affect transaction isolation guarantees and does not address the root cause of long-running operations. (15% 失败率)
