如何通过 Node.js MongoDB Driver API 将 x.509 证书主题作为用户添加到 Mongodb 3.4?

How to add x.509 certificate subject as a user to Mongodb 3.4 by Node.js MongoDB Driver API?

我需要使用已经具有 Node.js MongoDB 驱动程序 API 包的 Node.js 应用程序将用户添加到我的 MongoDB 3.4 副本集。

问题是:The API documentation doesn't cover how to add x.509 Certificate subject as a User.

有人知道怎么做吗?换句话说,我需要一个 Node.js mechanism/API 可以用来执行下面的 mongodb 命令:

mongo --host mongo-node-0
use admin
db.getSiblingDB("$external").runCommand(
{createUser: "emailAddress=foo@bar.com,CN=admin,OU=Clients,O=FOO,L=Dublin,ST=Ireland,C=IE",
roles: [
{ role: "userAdminAnyDatabase", db: "admin" },
{ role: "dbAdminAnyDatabase", db: "admin" },
{ role: "readWriteAnyDatabase", db:"admin" },
{ role: "clusterAdmin",  db: "admin" }
]})

按照 Mongo 文档,在 Node 上,针对 MongoDB 执行命令散列。这使您可以通过服务器上的 API 访问任何不可用的命令。

command(selector[, options], callback)
    Arguments:  

        selector (object) – the command hash to send to the server, ex: {ping:1}.
        [options] (object) – additional options for the command.
        callback (function) – this will be called after executing this method. The command always return the whole result of the command as the second parameter.

    Returns:    

    null

那么,你可以试试看:

 var db = new Db('$external', new MongoServer('localhost', 27017));
 db.open(function(err, db) {
   if (err) {
     console.log(err);
   }

   db.command({
     createUser: "emailAddress=foo@bar.com,CN=admin,OU=Clients,O=FOO,L=Dublin,ST=Ireland,C=IE",
     roles: [
       { role: "userAdminAnyDatabase", db: "admin" },
       { role: "dbAdminAnyDatabase", db: "admin" },
       { role: "readWriteAnyDatabase", db:"admin" },
       { role: "clusterAdmin",  db: "admin" }
     ]}, function(err, result){
       if (err) {
         console.log(err);
       }
       console.log(result)

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