mongodb
runtime_error
ai_generated
true
MongoServerError: ChangeStream: 来自集合删除之前的恢复令牌无效
MongoServerError: ChangeStream: resume token from before the collection drop is invalid
ID: mongodb/change-stream-resume-from-before-drop
90%修复率
86%置信度
1证据数
2024-03-01首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| MongoDB 5.0 | active | — | — | — |
| MongoDB 6.0 | active | — | — | — |
| MongoDB 7.0 | active | — | — | — |
根因分析
变更流尝试使用一个早于集合删除的恢复令牌,由于命名空间被重新创建,该令牌失效。
English
A change stream attempted to resume using a resume token that predates a collection drop, which invalidates the token because the namespace was recreated.
官方文档
https://www.mongodb.com/docs/manual/changeStreams/解决方案
-
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 } }) -
Restart the change stream from the latest event after a drop by not providing a resume token: db.watch() without resumeAfter
-
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() } }
无效尝试
常见但无效的做法:
-
100% 失败
The drop event changes the namespace's history, making pre-drop tokens invalid.
-
90% 失败
Resume tokens are tied to the namespace and oplog position.
-
95% 失败
The error is caused by the drop event, not by oplog expiration.