node.js mongodb 中的 db.getUser [英] db.getUser in node.js mongodb

查看:54
本文介绍了node.js mongodb 中的 db.getUser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Node.js MongoDB 2.x 中 MongoDB shell 命令 db.getUserdb.getUsers 的等价物是什么?我在驱动程序文档中看到 db.addUserdb.removeUser 存在但没有提到 getUsergetUsers.我唯一的选择是使用 db.command(相当于 mongo shell 命令 db.runCommand AFAICT)和 usersInfo 命令来查找用户.事情是这样的:

What is the equivalent of the MongoDB shell command db.getUser and db.getUsers in Node.js MongoDB 2.x? I see in the driver documentation that db.addUser and db.removeUser exist but no mention of getUser or getUsers. The only alternative I have is to use the db.command (equivalent of mongo shell command db.runCommand AFAICT) with usersInfo command to find the users. This goes like this:

var cmd = { usersInfo: 'marty', db: 'DbName'};
db.command(cmd, function (err, r) {
 assert.equal(null, err);

});

另外为了确保没有误会,以下page 没有回答这个问题,因为那个人使用的是他们自己的集合而不是数据库用户.除非我在 MongoDB 中遗漏了一些东西并且它适用于数据库用户(如果有人也能澄清我会很高兴).

Also to make sure there is no misunderstanding, the following page does not answer the question since that person was using their own collection and not the db users. Unless I'm missing something in MongoDB and it works with DB users (which I would be glad if someone can clarify as well).

感谢您的帮助!!

推荐答案

虽然这个问题是 5 年前提出的,但现在我想回答一下.所以我正在使用版本为 3.5.6 的 mongodb 本机驱动程序,这是我的解决方案.

Although this question is asked 5 years ago, I'd like to answer it now. So I'm using mongodb native driver of version: 3.5.6 and here's my solution.

let mongoClient = new MongoClient(url, { useNewUrlParser: true, useUnifiedTopology: true });

mongoClient.connect(function(err, client){
    assert.equal(null, err);
    client.db('dbName').command({usersInfo:1 }).then((userList)=>{
        console.log('user List',util.inspect(userList, {showHidden: false, depth: null}));
    })
    .catch((err)=>{
        console.log(err);
    });
}

希望这会有所帮助!

这篇关于node.js mongodb 中的 db.getUser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆