node.js - Mongodb Node JS serverStatus 始终返回 false

标签 node.js mongodb

我是 mongo 新手,正在摆弄管理服务器状态。对我来说,当我期待一个对象时它总是返回 false。我正在运行 mongod 并启用了身份验证。是否需要特定的用户权限?除了 Nodejs 驱动程序或 mongodb 手册中的 API 文档提供的内容之外,我找不到太多文档。

MonogDB 3.4 NodeJS 驱动程序 2.2

var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://user:pass@localhost:27017/test?authMechanism=DEFAULT&authSource=test', function(err, db) {
  var adminDB = db.admin();
  adminDB.serverStatus(function(err, status) {
    console.log(status);
    db.close();
  });
});

谢谢!

最佳答案

我找到了答案。对于启用了身份验证的数据库,您需要使用 serverStatus 操作向执行用户授予角色。

db.createRole(
   {
     role: "mongostatRole",
     privileges: [
       { resource: { cluster: true }, actions: [ "serverStatus" ] }
     ],
     roles: []
   }
)

关于node.js - Mongodb Node JS serverStatus 始终返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41917525/

相关文章:

mongodb - 连接到 MongoDB Atlas Cluster 错误

java - 如何将 SSL 证书添加到 Bluemix java Cloud Foundry 应用程序?

mongodb - 使用聚合管道创建名称基于子文档值的 MongoDB 字段?

c++ - 在C++中使用回调异步运行linux命令

mysql - 将 MySQL 变量与 Sequelize 结合使用

javascript - Node.js Mongo 使用 $position 而 $push-ing 无法按预期工作

javascript - Gulp.js 在没有 browserSync 的情况下提供 src 文件?

mongodb - 在opensuse上安装mongodb时出错

node.js - 类型错误 : Cannot read property 'findOne' of undefined

node.js - Mongoose 关系设计