# MongoServerError: ChangeStream: 来自集合删除之前的恢复令牌无效

- **ID:** `mongodb/change-stream-resume-from-before-drop`
- **领域:** mongodb
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

变更流尝试使用一个早于集合删除的恢复令牌，由于命名空间被重新创建，该令牌失效。

## 版本兼容性

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

## 解决方案

1. ```
   Listen for 'drop' events in the change stream cursor and save a new resume token after the collection is recreated: const cursor = db.watch(); cursor.on('change', (change) => { if (change.operationType === 'drop') { // wait for collection recreation and save new token } })
   ```
2. ```
   Restart the change stream from the latest event after a drop by not providing a resume token: db.watch() without resumeAfter
   ```
3. ```
   Store the resume token in a separate collection and implement a fallback to start from 'now' if the token is invalid: try { db.watch({ resumeAfter: savedToken }) } catch (e) { if (e.code === 280) { db.watch() } }
   ```

## 无效尝试

- **** — The drop event changes the namespace's history, making pre-drop tokens invalid. (100% 失败率)
- **** — Resume tokens are tied to the namespace and oplog position. (90% 失败率)
- **** — The error is caused by the drop event, not by oplog expiration. (95% 失败率)
