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

MongoDB クライアントの作成

MongoDB 配置に接続するには、次の 2 つのものが必要です。

  • 接続 URI。接続文字列とも呼ばれます。これは、接続するMongoDBデプロイをRustドライバーに指示します。

  • クライアントインスタンス。MongoDBMongoDBデプロイへの接続を作成し、それに対して操作を実行できるようにします。

これらのコンポーネントのいずれかを使用して、 MongoDB .に接続する際のRustドライバーの動作をカスタマイズすることもできます。

このガイドでは、接続string を作成し、{0Client インスタンスを使用して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(())
}

使用可能な接続オプションの詳細については、 「接続オプションの指定」ガイドを 参照してください 。

Tip

クライアントの再利用

クライアントを複数のセッションやオペレーションで再利用することで、パフォーマンスを向上させることができます。 毎回新しいインスタンスを 1 つ作成するのではなく、同じClientインスタンスを使用して複数のタスクを実行できます。 Client型は、複数のスレッドや非同期タスクによって安全に同時使用できます。

Rustドライバーを使用して Clientインスタンスを作成する方法の詳細については、次のAPIドキュメントを参照してください。

戻る

はじめる

項目一覧