Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs 菜单
Docs 主页
/ /

创建MongoDB客户端

要连接到 MongoDB 部署,您需要满足两个条件:

  • 连接 URI,也称为连接字符串,它告诉Rust驾驶员要连接到哪个MongoDB 部署。

  • 客户端实例,用于创建与MongoDB 部署的连接并允许您对其执行操作。

您还可以使用这些组件来自定义Rust驾驶员在连接到MongoDB时的行为方式。

本指南介绍如何创建连接string并使用 Client实例连接到MongoDB 。

连接 URI(也称为连接字符串)可告知驱动程序如何连接到 MongoDB,以及连接后如何进行操作。

标准连接string包括以下组件:

组件
说明

mongodb://

必需。将其标识为标准连接格式中字符串的前缀。

<db_username>:<db_password>

可选。 身份验证凭证。 如果包含这些内容,客户端将根据 authSource 中指定的数据库对用户进行身份验证。 有关authSource 连接选项的更多信息,请参阅《 身份验证机制》指南。

hostname[:port]

必需。运行MongoDB 的主机名和可选端口号。如果不包含端口号,驱动程序将使用默认端口 27017

/defaultauthdb

可选。如果连接字符串包含身份验证凭证但不包含 authSource 选项,则要使用的身份验证数据库。

?<options>

可选。一个查询字符串,它将特定于连接的选项指定为<name>=<value> 对。有关这些选项的完整说明,请参阅“指定连接选项”指南。

有关创建连接string 的更多信息,请参阅 MongoDB Server文档中的 连接字符串 。

客户端管理连接并运行数据库命令。要创建 Client实例,请将连接字符串传递给 Client::with_uri_str() 方法,如以下示例所示:

let client = Client::with_uri_str(uri)?;

创建客户端时,您可以通过将 ClientOptions对象传递给 with_options() 方法来指定连接选项。

还可以通过将连接字符串传递给 ClientOptions 结构体的 parse() 方法,在连接字符串中指定连接选项。

以下代码示例展示如何创建客户端、指定 Stable API连接选项、连接到MongoDB以及验证连接是否成功。选择下方的“异步API”或“同步API”标签页,查看相应的代码示例:

use mongodb::{ bson::doc, options::{ ClientOptions, ServerApi, ServerApiVersion }, Client };
#[tokio::main]
async fn main() -> mongodb::error::Result<()> {
// Replace the placeholder with your Atlas connection string
let uri = "<connection string>";
let mut client_options = ClientOptions::parse(uri).await?;
// Set the server_api field of the client_options object to Stable API version 1
let server_api = ServerApi::builder().version(ServerApiVersion::V1).build();
client_options.server_api = Some(server_api);
// Create a new client and connect to the server
let client = Client::with_options(client_options)?;
// Send a ping to confirm a successful connection
client.database("admin").run_command(doc! { "ping": 1 }).await?;
println!("Pinged your deployment. You successfully connected to MongoDB!");
Ok(())
}
use mongodb::{ bson::doc, options::{ ClientOptions, ServerApi, ServerApiVersion }, sync::Client };
fn main() -> mongodb::error::Result<()> {
// Replace the placeholder with your Atlas connection string
let uri = "<connection string>";
let mut client_options = ClientOptions::parse(uri).run()?;
// Set the server_api field of the client_options object to Stable API version 1
let server_api = ServerApi::builder().version(ServerApiVersion::V1).build();
client_options.server_api = Some(server_api);
// Create a new client and connect to the server
let client = Client::with_options(client_options)?;
// Send a ping to confirm a successful connection
client.database("admin").run_command(doc! { "ping": 1 }).run()?;
println!("Pinged your deployment. You successfully connected to MongoDB!");
Ok(())
}

有关可用连接选项的更多信息,请参阅“指定连接选项”指南。

提示

客户端重用

可以通过在各种会话和操作中重复使用客户端来提高性能。 您可以使用同一 Client 实例来执行多项任务,而不是每次都创建一个新实例。 Client 类型可供多个线程或异步任务安全地并发使用。

要学习;了解有关使用Rust驾驶员创建 Client实例的详情,请参阅以下API文档:

后退

开始体验

在此页面上