Tarang
Tarang

Reputation: 75975

MongoDB query database for time

Is there a way to query mongodb in such a way that I can retrieve the time?

I often do a check where I check the time the mongodb server is running on before putting data on. I need to know the time on the db server from the point of view of a client.

I'm using the C# .NET wrapper, but if there is a query that can do this on any platform that would be nice too. How would would I get the db time?

I know there are obviously other ways to do this, by synchronizing times. But I need to use this option so there can be no tampering with the local time

Upvotes: 5

Views: 4536

Answers (2)

JohnnyHK
JohnnyHK

Reputation: 312129

db.hostInfo() returns an object where system.currentTime is the current time on the server's host.

Upvotes: 6

jjm
jjm

Reputation: 6198

I'm not sure what you mean by "from the point of view of a client". But, this might help:

>> db.serverStatus()

returns a big object, which includes the time. Apparently (haven't tested this), for the java driver you can get this with Db.command("serverStatus"), so I would imagine something similar works in C#.

References:

Mongo docs for serverStatus

An answered question about serverStatus in Java

Upvotes: 1

Related Questions