Reputation: 8806
Is there a way to get the MongoDB server datetime using nodejs? Please understand that what I need is not to add the timestamp to a field in a document but to retrieve the date and time from the MongoDB Atlas server to be sent in the nodejs response.
I've tried,
client = new MongoClient(uri, {
useNewUrlParser: true
});
client.connect().then(function () {
var ob = client.db("dbname").runCommand({
serverStatus: 1,
repl: 1
});
res.send(ob);
});
but this gives me,
TypeError: client.db(...).runCommand is not a function
Upvotes: 2
Views: 376
Reputation: 103435
You need to run the command in the admin database and prior to the operation you need to grant a role to the executing user with the serverStatus
action
client = new MongoClient(uri, { useNewUrlParser: true });
client.connect().then(function() {
const adminDb = client.db("dbName").admin();
adminDb.serverStatus(function(err, status) {
res.send(status.localTime);
client.close();
});
});
Upvotes: 2