Query and clusterTime

Hi all,

I’m trying to use the events stream with the golang drivers. What I’m trying to achieve is:

  1. find all documents of a collection + get the current clusterTime in an atomic operation
  2. start a watcher with the clusterTime obtained at step 1

This way, we are sure to not miss events after we read the collection a first time.

I’m failing to do that.
If I use a runCommand(find), I get some documents + clusterTime, but I can’t reuse the cursor to iterate over all documents.
If I just do a runCommand(ping) I get the clusterTime, but I can’t find a way to use readConcern=snapshot + atClusterTime with the golang drivers.

Is there a way to achieve what I’m trying to do in general, and with the golang drivers specifically?

Thx for your help.

I think the functionality you’re looking for was just released in version 1.7.0 of the Go driver:

1 Like

Thx for the info (and sorry for the delay of my response).

So with the latest version it works as you said, but there is a trick: you have to use a deprecated interface to access the cluster time corresponding to the find (mongo.XSession).

Hope this small code can help people in the future:

session, err := conn.StartSession(options.Session().SetSnapshot(true))
if err != nil {
defer session.EndSession(ctx)

err = mongo.WithSession(ctx, session, func(sessionContext mongo.SessionContext) error {
    db := conn.Database(dbName)
    collection := db.Collection(collectionName)
    cursor, err := collection.Find(sessionContext, query = bson.M{})
    // do things
    return nil
if err != nil {

snapshotTime := session.(mongo.XSession).ClientSession().SnapshotTime