通过nodejs在MongoDB中授予用户角色

时间:2016-07-16 17:39:16

标签: node.js mongodb roles

我正在尝试在Node.JS中编写代码,为MongoDB中的用户授予角色。 我通过CLI了解一种方法:

db.grantRolesToUser( "<username>", [ <roles> ], { <writeConcern> } )

我如何通过Node.JS来完成?

由于

1 个答案:

答案 0 :(得分:0)

我不知道这是唯一的方法,但我在文档中唯一能看到的是add a user时授予角色。

var MongoClient = require('mongodb').MongoClient,

test = require('assert'); MongoClient.connect('mongodb:// localhost:27017 / test',function(err,db){

// Use the admin database for the operation
  var adminDb = db.admin();

  // Add the new user to the admin database
  adminDb.addUser('admin11', 'admin11', {roles : ['blah']}, function(err, result) {

    // Authenticate using the newly added user
    adminDb.authenticate('admin11', 'admin11', function(err, result) {
      test.ok(result);

      adminDb.removeUser('admin11', function(err, result) {
        test.ok(result);

        db.close();
      });
    });
  });
});