Overview概述
In this guide, you can learn how to use a connection string and a 在本指南中,您可以学习如何使用连接字符串和MongoClient object to connect to different types of MongoDB deployments.MongoClient对象连接到不同类型的MongoDB部署。
Tip
To learn more about how to retrieve your connection string, see the Connect via Drivers guide in the Atlas documentation.要了解有关如何检索连接字符串的更多信息,请参阅Atlas文档中的《通过驱动程序连接》指南。
Atlas
To connect to a MongoDB deployment on Atlas, include the following elements in your connection string:要连接到Atlas上的MongoDB部署,请在连接字符串中包含以下元素:
URL of your Atlas clusterAtlas集群的URL- MongoDB
username用户名 - MongoDB
password密码
Then, pass your connection string to the 然后,将连接字符串传递给MongoClient constructor.MongoClient构造函数。
When you connect to Atlas, we recommend using the Stable API client option to avoid breaking changes when Atlas upgrades to a new version of MongoDB Server. To learn more about the Stable API feature, see the Stable API guide.当您连接到Atlas时,我们建议使用Stable API客户端选项,以避免Atlas升级到新版本的MongoDB Server时破坏更改。要了解有关Stable API功能的更多信息,请参阅Stable API指南。
The following code shows how to use the Node.js driver to connect to an Atlas cluster. The code also uses the 以下代码显示了如何使用Node.js驱动程序连接到Atlas集群。代码还使用server_api field to specify a Stable API version.server_api字段来指定稳定的api版本。
const { MongoClient, ServerApiVersion } = require("mongodb");
// Replace the placeholder with your Atlas connection string
const uri = "<connection string>";
// Creates a MongoClient with a MongoClientOptions object to set the Stable API version使用MongoClientOptions对象创建MongoClient以设置Stable API版本
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
}
);
async function run() {
try {
// Connects the client to the server (optional starting in v4.7)将客户端连接到服务器(从v4.7开始可选)
await client.connect();
// Sends a ping to confirm a successful connection发送ping确认连接成功
await client.db("admin").command({ ping: 1 });
console.log("Pinged your deployment. You successfully connected to MongoDB!");
} finally {
// Ensures that the client will close when you finish/error确保客户端在您完成/出错时关闭
await client.close();
}
}
run().catch(console.dir);Local Deployments本地部署
To connect to a local standalone MongoDB deployment, specify the host of the server. Optionally, specify the port of the server. If no port is specified, the default port is 要连接到本地独立的MongoDB部署,请指定服务器的主机。(可选)指定服务器的端口。如果未指定端口,则默认端口为27017.27017。
You can specify the host and port to connect to by using a connection string, as shown in the following code:您可以使用连接字符串指定要连接的主机和端口,如以下代码所示:
const client = new MongoClient("mongodb://host1:27017");
You can also specify your host as 您还可以将主机指定为localhost. The following code example connects to localhost on the specified port:localhost。以下代码示例连接到指定端口上的localhost:
const client = new MongoClient("mongodb://localhost:27017");Replica Sets复制集
To connect to a replica set, we recommend that you specify all nodes that are part of the replica set. If one or more nodes becomes unavailable, specifying all nodes allows the driver to still connect to the replica set if one node is available.要连接到副本集,我们建议您指定作为副本集一部分的所有节点。如果一个或多个节点不可用,指定所有节点允许驱动程序在一个节点可用的情况下仍然连接到副本集。
However, it is sufficient to pass the address of any one node in the replica set to the driver. The node does not need to be the primary, and it may be a hidden node. The driver will then automatically discover the remaining nodes.但是,将副本集中任何一个节点的地址传递给驱动程序就足够了。该节点不需要是主节点,也可以是隐藏节点。然后,驱动程序将自动发现剩余的节点。
The following example shows how to connect to the replica set by using a connection string and how to verify the replica set name on connection by using the 以下示例显示了如何使用连接字符串连接到副本集,以及如何使用replicaSet connection string option:replicaSet连接字符串选项在连接时验证副本集名称:
const client = new MongoClient("mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myRs");
Note
Replica Set in DockerDocker中的副本集
When a replica set runs in Docker, it might expose only one MongoDB endpoint. In this case, the replica set is not discoverable. Specifying 当一个副本集在Docker中运行时,它可能只暴露一个MongoDB端点。在这种情况下,无法发现副本集。在连接URI中指定directConnection=false in your connection URI, or leaving this option unset, can prevent your application from connecting to it.directConnection=false,或者不设置此选项,可能会阻止您的应用程序连接到它。
In a test or development environment, you can connect to the replica set by specifying 在测试或开发环境中,可以通过指定directConnection=true. In a production environment, we recommend configuring the cluster to make each MongoDB instance accessible outside of the Docker virtual network.directConnection=true连接到副本集。在生产环境中,我们建议配置集群,使每个MongoDB实例都可以在Docker虚拟网络之外访问。
DNS Service DiscoveryDNS服务发现
To use DNS service discovery to look up the DNS SRV record of the service you're connecting to, specify the SRV connection format in your connection string. If you specify this format, the Node.js driver automatically rescans for new hosts. Your deployment can add hosts to its topology without requiring changes in your client configuration.若要使用DNS服务发现查找所连接服务的DNS SRV记录,请在连接字符串中指定SRV连接格式。如果指定此格式,Node.js驱动程序将自动重新扫描新主机。您的部署可以将主机添加到其拓扑中,而不需要更改客户端配置。
The following code shows a connection string that uses the SRV connection format:以下代码显示了使用SRV连接格式的连接字符串:
const uri = "mongodb+srv://<hostname>/"
To learn more about the SRV connection format, see the SRV Connection Format entry in the MongoDB Server manual.要了解有关SRV连接格式的更多信息,请参阅MongoDB服务器手册中的SRV连接格式条目。
API Documentation文档
To learn more about creating a 要了解有关使用Node.js驱动程序创建MongoClient object with the Node.js driver, see the API documentation for MongoClient .MongoClient对象的更多信息,请参阅MongoClient的API文档。