AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。

MongoClient の作成

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

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

  • MongoDBデプロイへの接続を作成し、操作を実行する mongoc_client_t 構造。

このガイドでは、接続文字列を作成し、mongoc_client_t 構造を使用してMongoDBに接続する方法について説明します。

標準の接続stringには次のコンポーネントが含まれます。

コンポーネント
説明

mongodb+srv://

必須。 これを SRV 接続形式の string として識別するプレフィックス。

username:password

任意。 認証資格情報。 これらを含めると、クライアントはauthSource で指定されたデータベースに対してユーザーを認証します。 authSource接続オプションの詳細については、 認証メカニズム のガイドをご覧ください。

host[:port]

必須。 MongoDB が実行されているホストとオプションのポート番号。 ポート番号を指定しない場合、ドライバーはデフォルトのポート27017を使用します。

/defaultauthdb

任意。 接続stringに username:password@ 認証情報が含まれている一方で、authSource オプションが含まれていない場合に使用する認証データベース。 このコンポーネントを含めない場合、クライアントはadminデータベースに対してユーザーを認証します。

?<options>

任意。接続固有のオプションを <name>=<value> ペアとして指定するクエリ文字列です。

接続 の作成の詳細については、stringMongoDB Server ドキュメントの「 接続 文字列 」を参照してください。

Atlas 上の MongoDB 配置に接続するには、まずクライアントを作成する必要があります。

接続 URI を string として mongoc_client_new() 関数に渡して、 MongoDBインスタンスに接続できます。

// Initialize the C Drivermongoc_init(); // Create a new client and connect to the servermongoc_client_t *client = mongoc_client_new("mongodb+srv://<db_username>:<db_password>@<hostname:port>/?<options>");mongoc_database_t *database = mongoc_client_get_database(client, "admin"); bson_t *ping = BCON_NEW("ping", BCON_INT32(1));bson_t reply = BSON_INITIALIZER;bson_error_t error; // Send a ping to confirm a successful connectionif (!mongoc_client_command_simple(client, "admin", ping, NULL, &reply, &error)) { fprintf(stderr, "error: %s\n", error.message); goto cleanup;} printf("Pinged your deployment. You successfully connected to MongoDB!\n"); cleanup: bson_destroy(&reply); bson_destroy(ping); mongoc_database_destroy(database); mongoc_client_destroy(client); // Cleanup the C Driver mongoc_cleanup();

新しいサーバーバージョンにアップグレードするときに重大な変更を回避するために、Stable APIバージョンのクライアントオプションを設定できます。

Tip

Stable API 機能について詳しくは、「 Stable APIガイド 」をご覧ください。

次のコードは、Atlas 上の MongoDB 配置に接続するときに接続文字列と Stable API クライアント オプションを指定し、接続が成功したことを確認する方法を示します。

// Intialize the MongoDB C Drivermongoc_init(); // Create a new client and connect to the servermongoc_client_t *client = mongoc_client_new("mongodb+srv://<db_username>:<db_password>@<hostname:port>/?<options>"); // Set the version of the Stable API on the clientmongoc_server_api_t *api = mongoc_server_api_new(MONGOC_SERVER_API_V1); bson_t *ping = BCON_NEW("ping", BCON_INT32(1));bson_t reply = BSON_INITIALIZER;bson_error_t error; if (!mongoc_client_set_server_api(client, api, &error)) { fprintf(stderr, "error: %s\n", error.message); goto cleanup;} // Send a ping to confirm a successful connectionif (!mongoc_client_command_simple(client, "admin", ping, NULL, &reply, &error)) { fprintf(stderr, "error: %s\n", error.message); goto cleanup;} printf("Pinged your deployment. You successfully connected to MongoDB!\n"); cleanup: bson_destroy(&reply); bson_destroy(ping); mongoc_server_api_destroy(api); mongoc_client_destroy(client); // Cleanup the C Driver mongoc_cleanup();

mongoc_client_t 構造の詳細については、APIドキュメントを参照してください。