从MongoDB地图集-Node.js

时间:2019-07-06 07:34:55

标签: node.js mongodb mongodb-atlas

是否有一种使用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

1 个答案:

答案 0 :(得分:2)

您需要在管理数据库中运行命令,并且在执行该操作之前,需要通过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();
    });
});