无法在服务器 NodeJS 上的 Mongo 中创建用户(或基于自定义的角色) [英] Impossible to create users (or custom-based roles) in Mongo on server NodeJS

查看:51
本文介绍了无法在服务器 NodeJS 上的 Mongo 中创建用户(或基于自定义的角色)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试直接从我们的 Express 服务器在我的数据库上创建用户,使用 Mongo 3.4 作为数据库.这是现在服务器的代码:

I am trying to create users on my database directly from our Express server, using Mongo 3.4 for the database. Here is the code for the server for now:

var express = require('express');

var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://mongo:27017/myDb';

var dbvotes = "collection1";
var dbusers = "collection2";


//Functions for users
app.post('/newUser', function(req, res, db) {

    MongoClient.connect(url, function(err, db){

        //Ecriture dans la base user
        db.collection(dbusers).insertOne( {
            "name" : req.body.name,
            "surname" : req.body.surname,
            "username" : username,
            "password" : "pasadmin",
         });

        //Creation of the user in the DB
        db.createUser( { 'user': username, 'pwd': "pasadmin", roles: [] });


        db.close();
        });
    console.log("Inserted a new user in the database.");
    res.send("User added. Click precedent to add a new user.");
});

然而,每当客户端尝试插入一个新用户时,它实际上是在用户集合中创建的,但不是作为数据库的用户,因为我收到以下错误:TypeError: db.createUser is not a functionweb_1 |在/app/app.js:47:6.

However, whenever the client tries to insert a new user, it is effectively created in the user collections, but not as a user of the database as I get the following error: TypeError: db.createUser is not a function web_1 | at /app/app.js:47:6.

我尝试使用 db.addUser 代替,但是,自 Mongo 2.6 起此方法已被弃用,并且也不起作用,并且 db.adminCommand 提供相同的错误,db.adminCommand 不是函数.在此之前,我努力使用 Node 在数据库中创建自定义角色,并决定也通过 Mongo shell 来完成,但在数据库中逐一添加用户时,这不是一个选项.

I have tried to user db.addUser instead, however, this is deprecated since Mongo 2.6 and not working either, and db.adminCommand serves the same error, db.adminCommand is not a function. Before that, I struggled to create custom roles in the database with Node and decided to do it via the Mongo shell as well, but it's not an option when it comes to adding user 1 by 1 in the database.

当我在 Mongo shell 中使用这些函数时,命令正在工作,所以我想它来自我在服务器中实现 Mongo 的方式(通过 Docker),或者由于 Javascript 的一些限制.知道它是什么吗?

When I use these functions in the Mongo shell, the commands are working, so I suppose it comes from the way I implemented Mongo within the server (via Docker), or due to some limitations with Javascript. Any idea what could it be?

提前感谢社区!

推荐答案

正确的命令是:

db.addUser( username, password, { roles: [ role ] } );

其中 role 是一些 MongoDB 角色.更多信息可以从源文件中找到/a>.也可以是{ role: , db: ;},其中role为MongoDB角色,db为数据库字符串名.

Where role is some MongoDB role. More info can be found from the source file. It can also be an object in the formation of { role: <string>, db: <string> }, where role is a MongoDB role and db is the string name of the database.

您也可以使用 db.admin().addUser.如果用户可以访问多个数据库,或者您想要用户的中心位置,这将是合乎逻辑的选择.

You can also use db.admin().addUser. This would be the logical choice if the user has access to multiple databases or you want a central location of your users.

但是,除非您正在开发实际的管理工具,否则我无法想象从您的应用程序添加系统用户是个好主意.添加到数据库的普通用户"将位于您自己的用户集合中.系统用户 是可以直接访问您的数据库的人.

However, I can't imagine it's a good idea to add system users from your application unless you're developing an actual administrative tool. Normal "users" added to a database would be in your own users collection. A system user is someone who has direct access to your database.

这篇关于无法在服务器 NodeJS 上的 Mongo 中创建用户(或基于自定义的角色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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