有没有办法使用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/