Search code examples
c#mongodbmongodb-.net-driver

How to get the cluster time in MongoDB using the C# driver


So IClientSessionHandle.ClusterTime returns null for me:

using (var session = await client.StartSessionAsync())
{
   var time = session.ClusterTime; // null
}

I would be more than happy to get the result of Date() from the server, but it completely eludes me how something so simple can be done. I'm not interested in whether this is a good idea or not, I just want to know what the server thinks the current time is.


Solution

  • I think this value is not what you need. It returns the last seen clusterTime when an operation was executed. See this doc. You may see more detailed $clusterTime description here

    Look at db.runCommand({ isMaster: 1 }) response and in particular at $localTime