gpt4 book ai didi

node.js - 从 nodejs 连接到 mongodb 时使用 authenticationDatabase 选项

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

当我使用 mongo 从 shell 连接到 mongodb 时,我需要使用以下命令mongo -u xxxx -p xxxx --authenticationDatabase admin 然后我获得了完全访问权限...但是在从 nodejs 连接时我找不到指定 authenticationDatabase 的方法这是我的代码

var MongoCli = require('mongodb').MongoClient;
var url = "mongodb://127.0.0.1:27017/contactapp";

MongoCli.connect(url,function(err,db){
if(err)
{
console.log('Unable to connect');
}
else
{
console.log('Connected at ',url);
db.authenticate('username','password','--authenticationDatabase admin',function(err,res){
if(err)
{
console.log('Error');
}
else
{
console.log('Auth Success');
var collection = db.collection('contact');
collection.find().toArray(function(err,res){
if(err)
{
console.log(err);
}
else if(res.length)
{
console.log('Found ',res);
}
else
{
console.log('No data found');
}
db.close();
});
}
});
}
});

我需要使用authenticationDatabase。提前致谢。

最佳答案

试着改变这个:

db.authenticate('username','password','--authenticationDatabase admin',function(err,res){

这是:

db.admin().authenticate('username', 'password', function(err,res){

关于node.js - 从 nodejs 连接到 mongodb 时使用 authenticationDatabase 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33320264/

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