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

标签 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

最佳答案

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

关于node.js - 从 MongoDB Atlas 获取日期时间 - Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56912131/

相关文章:

node.js - 如何将 gridStore 替换为 gridFSBucket?

node.js - 通过 react 在特快专递中进行重定向

node.js - 在单个 Azure 应用程序中使用 Express 后端来 React 应用程序(通过 create-react-app)?

mongodb - 如何从 mongo 集合中的数组属性中删除元素

Mongodb atlas搜索: how to make use of multiple analyzers for the same query string,以便至少返回一些结果

node.js - 为什么npx每次都安装webpack?

javascript - JS mongoDB 按 previousItem 属性排序(父子关系)

mongodb - Mongo 项目测试值是否在数组中

mongodb - 从本地迁移到 MongoDb Atlas