Getting the last change in the oplog (Changestream _id format)

24 views
Skip to first unread message

Kevin Fairclough

unread,
Sep 27, 2018, 4:28:23 AM9/27/18
to mongodb-user
Hi

Odd request but here goes.

I need to be able to determine the last change that resides in the oplog and build a changestream _id from it.

This is so i can obtain changes to the database from a certain point in time (i.e. when i manually copy the database).  I'm using $gt changestream._id to gather changes.

Is there any way to manually build the changestream._id?

Regards
Kevin Fairclough

Wan Bachtiar

unread,
Sep 27, 2018, 9:14:18 PM9/27/18
to mongodb-user

This is so i can obtain changes to the database from a certain point in time (i.e. when i manually copy the database). I’m using $gt changestream._id to gather changes.

Hi Kevin,

There are two ways that you could do without trying to peek into the oplog directly. 

You can specify resumeAfter option on collection.watch(). This will direct watch() to attempt resuming notifications starting after the operation specified in the resume token. The option accepts value returned by change stream event in _id field.

Alternatively you can specify startAtOperationTime option on collection.watch() (new in MongoDB v4.0). The option accepts a timestamp value returned by change stream event in clusterTime field.

For both of the solutions above, you need to collect the last change stream event known before performing the manual database copy.

Regards,
Wan.

Reply all
Reply to author
Forward
0 new messages