mongodb runtime_error ai_generated true

MongoServerError: ChangeStream: resume token from before the collection drop is invalid

ID: mongodb/change-stream-resume-from-before-drop

Also available as: JSON · Markdown · 中文
90%Fix Rate
86%Confidence
1Evidence
2024-03-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
MongoDB 5.0 active
MongoDB 6.0 active
MongoDB 7.0 active

Root Cause

A change stream attempted to resume using a resume token that predates a collection drop, which invalidates the token because the namespace was recreated.

generic

中文

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

Official Documentation

https://www.mongodb.com/docs/manual/changeStreams/

Workarounds

  1. 85% success 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 } })
    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. 95% success Restart the change stream from the latest event after a drop by not providing a resume token: db.watch() without resumeAfter
    Restart the change stream from the latest event after a drop by not providing a resume token: db.watch() without resumeAfter
  3. 90% success 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() } }
    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() } }

中文步骤

  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() } }

Dead Ends

Common approaches that don't work:

  1. 100% fail

    The drop event changes the namespace's history, making pre-drop tokens invalid.

  2. 90% fail

    Resume tokens are tied to the namespace and oplog position.

  3. 95% fail

    The error is caused by the drop event, not by oplog expiration.