从 MongoDB Atlas 获取日期时间 - Node.js

Get datetime from MongoDB Atlas - Node.js

有没有办法使用 nodejs 获取 MongoDB 服务器日期时间?请理解,我需要的不是将时间戳添加到文档中的字段,而是从 MongoDB Atlas 服务器检索要在 nodejs 中发送的日期和时间响应。


我试过了,

client = new MongoClient(uri, {
    useNewUrlParser: true
});
client.connect().then(function () {
    var ob = client.db("dbname").runCommand({
        serverStatus: 1,
        repl: 1
    });
    res.send(ob);
});

但这给了我,

TypeError: client.db(...).runCommand is not a function

您需要 运行 管理数据库中的命令,并且在操作之前,您需要使用 serverStatus 操作

向执行用户授予角色
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();
    });
});