gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:08:20 25 4
gpt4 key购买 nike

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com