node.js 如何使用 Server 实例指定 mongodb 用户名和密码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14045509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:50:17  来源:igfitidea点击:

How can you specify the mongodb username and password using a Server instance?

node.jsmongodb

提问by Oved D

The MongoClientdocumentation shows how to use a Server instance to create a connection:

MongoClient文档说明如何使用服务器实例来创建连接:

var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server;

// Set up the connection to the local db
var mongoclient = new MongoClient(new Server("localhost", 27017));

How would you specify a username and password for this?

您将如何为此指定用户名和密码?

回答by Mattias

There are two different ways you can do this

有两种不同的方法可以做到这一点

#1

#1

Documentation(Note the example in the documentation uses the Db object)

文档(注意文档中的示例使用了 Db 对象)

// Your code from the question

// Listen for when the mongoclient is connected
mongoclient.open(function(err, mongoclient) {

  // Then select a database
  var db = mongoclient.db("exampledatabase");

  // Then you can authorize your self
  db.authenticate('username', 'password', function(err, result) {
    // On authorized result=true
    // Not authorized result=false

    // If authorized you can use the database in the db variable
  });
});

#2

#2

Documentation MongoClient.connect
Documentation The URL
A way I like much more because it is smaller and easier to read.

文档 MongoClient.connect
文档 URL
我更喜欢的一种方式,因为它更小且更易于阅读。

// Just this code nothing more

var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://username:password@localhost:27017/exampledatabase", function(err, db) {
  // Now you can use the database in the db variable
});

回答by Maxim Georgievskiy

Thanks Mattias for correct answer.

感谢马蒂亚斯的正确答案。

I would like to add that sometimes you have credentials from one database while want to connect to another. In that case, you can still use URL way to connect, just adding ?authSource=parameter to URL.

我想补充一点,有时您拥有一个数据库的凭据,但又想连接到另一个数据库。在这种情况下,您仍然可以使用 URL 方式进行连接,只需?authSource=在 URL 中添加参数即可。

For example, let say you have admin credentials from database admin, and want to connect to database mydb. You can do it the following way:

例如,假设您有来自 database 的管理员凭据admin,并且想要连接到 database mydb。您可以通过以下方式执行此操作:

const MongoClient = require('mongodb').MongoClient;

(async() => {

    const db = await MongoClient.connect('mongodb://adminUsername:adminPassword@localhost:27017/mydb?authSource=admin');

    // now you can use db:
    const collection = await db.collection('mycollection');
    const records = await collection.find().toArray();
    ...

})();

Also, if your password contains special characters, you still can use URL way like this:

此外,如果您的密码包含特殊字符,您仍然可以使用这样的 URL 方式:

    const dbUrl = `mongodb://adminUsername:${encodeURIComponent(adminPassword)}@localhost:27017/mydb?authSource=admin`;
    const db = await MongoClient.connect(dbUrl);

Note: In earlier versions, { uri_decode_auth: true }option was required (as second parameter to connectmethod) when using encodeURIComponentfor username or password, however now this option is obsolete, it works fine without it.

注意:在早期版本中,用于用户名或密码时{ uri_decode_auth: true }需要选项(作为connect方法的第二个参数)encodeURIComponent,但是现在此选项已过时,没有它也能正常工作。