Change stream. the resume token was not found

mongo version: 4.4.5

dms_admin> watch_cursor = db.getMongo().watch()
ChangeStreamCursor on mongodb://<credentials>@ip:port/?directConnection=true&appName=mongosh+1.6.2
dms_admin> watch_cursor.tryNext();
{
  _id: {
    _data: '8263EB034E000000032B022C0100296E5A1004D775448B6F1E4A91A72F8304F5786F03463C5F6964003C34653738316138632D643634632D343130302D623831612D386364363463373130306137000004'
  },
  operationType: 'replace',
  clusterTime: Timestamp({ t: 1676346190, i: 3 }),
  fullDocument: {
      xxxxx: xxxxx
  },
  ns: { db: 'db', coll: 'coll' },
  documentKey: { _id: '4e781a8c-d64c-4100-b81a-8cd64c7100a7' }
}
dms_admin> watchCursor = db.watch([], {"startAfter": {"_data": "8263EB034E000000032B022C0100296E5A1004D775448B6F1E4A91A72F8304F5786F03463C5F6964003C34653738316138632D643634632D343130302D623831612D386364363463373130306137000004"}});
ChangeStreamCursor on dms_admin
[direct: mongos] dms_admin> watchCursor.tryNext();
null
[direct: mongos] dms_admin> watchCursor.tryNext();
null
[direct: mongos] dms_admin> watchCursor = db.watch([], {"resumeAfter": {"_data": "8263EB034E000000032B022C0100296E5A1004D775448B6F1E4A91A72F8304F5786F03463C5F6964003C34653738316138632D643634632D343130302D623831612D386364363463373130306137000004"}});
ChangeStreamCursor on dms_admin
[direct: mongos] dms_admin> watchCursor.tryNext();
null
[direct: mongos] dms_admin> watchCursor.tryNext();
null
// before execute tryNext, modify some data at another session.
[direct: mongos] dms_admin> watchCursor.tryNext();
null
[direct: mongos] dms_admin> watchCursor.tryNext();
MongoServerError: cannot resume stream; the resume token was not found. {_data: "8263EB0393000000032B022C0100296E5A10042A041639D2024311906742C001F1320B463C5F6964003C4F35313233353933000004"}
[direct: mongos] dms_admin>

anyone can help?

Hi :wave: @rancho_zhang,

Welcome to the Community forums :sparkles:

Apologies for the late response!

If no operation has taken place in the database, watchCursor.tryNext(); will return null. However, if an operation has been executed, it will return the changes along with their details.

A change stream can be resumed using a resume token that points to a specific timestamp in the oplog. However, when the oplog rolls over, the resume token becomes invalid. Trying to resume a change stream with an invalid resume token will result in an error, as indicated above.

For more information on the resumability of the change stream check the documentation link.

Best,
Kushagra

1 Like