且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在node.js中对猫鼬连接mongodb进行身份验证

更新时间:2023-09-20 21:41:40

您必须在连接字符串中声明authSource参数,以便指定包含用户凭据的数据库的名称:

You must declare the authSource parameter in your connection string in order to specify the name of the database that contains your user's credentials:

var options = {
  user: "superuser",
  pass: "12345678",
  useMongoClient: true
};

var mongooseConnectionString = 'mongodb://localhost/twitter-mongo?authSource=admin';

请注意,我还将"useMongoClient"设置为true.这使请使用MongoClient进行身份验证并使用身份验证凭据进行连接,并且 open()已被弃用错误消息.

Note that I've also set "useMongoClient" to true. This silences the Please authenticate using MongoClient.connect with auth credentials and open() is deprecated error messages.