gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:48:29 25 4
gpt4 key购买 nike

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

问题是:The API documentation不包括如何 add x.509 Certificate subject as a User .

有人知道怎么做吗?换句话说,我需要一个 Node.js 机制/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();
});
});

关于node.js - 如何通过 Node.js MongoDB Driver API 将 x.509 证书主题作为用户添加到 Mongodb 3.4?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42948707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com