はじめに
公式 MongoDB C ドライバーのドキュメント サイトへようこそ。 アプリケーションにドライバーを追加して、C の MongoDB で動作できます。必要なライブラリ、 libmongocとlibbsonをダウンロードするには、 MongoDB C ドライバー ライブラリの取得手順に従ってください。または実行可能なプロジェクトを設定します。チュートリアルに従ってください。
MongoDB Atlas への接続
次の接続スニペットを使用して、Atlas 上の MongoDB 配置への接続をテストできます。
int main(void) { mongoc_client_t *client = NULL; bson_error_t error = {0}; mongoc_server_api_t *api = NULL; mongoc_database_t *database = NULL; bson_t *command = NULL; bson_t reply = BSON_INITIALIZER; int rc = 0; bool ok = true; // Initialize the MongoDB C Driver. mongoc_init(); client = mongoc_client_new("<connection string>"); if (!client) { fprintf(stderr, "Failed to create a MongoDB client.\n"); rc = 1; goto cleanup; } // Set the version of the Stable API on the client. api = mongoc_server_api_new(MONGOC_SERVER_API_V1); if (!api) { fprintf(stderr, "Failed to create a MongoDB server API.\n"); rc = 1; goto cleanup; } ok = mongoc_client_set_server_api(client, api, &error); if (!ok) { fprintf(stderr, "error: %s\n", error.message); rc = 1; goto cleanup; } // Get a handle on the "admin" database. database = mongoc_client_get_database(client, "admin"); if (!database) { fprintf(stderr, "Failed to get a MongoDB database handle.\n"); rc = 1; goto cleanup; } // Ping the database. command = BCON_NEW("ping", BCON_INT32(1)); ok = mongoc_database_command_simple( database, command, NULL, &reply, &error ); if (!ok) { fprintf(stderr, "error: %s\n", error.message); rc = 1; goto cleanup; } bson_destroy(&reply); printf("Pinged your deployment. You successfully connected to MongoDB!\n"); // Perform cleanup. cleanup: bson_destroy(command); mongoc_database_destroy(database); mongoc_server_api_destroy(api); mongoc_client_destroy(client); mongoc_cleanup(); return rc; }
この接続スニペットは、 Stable API 機能を使用します。 この機能にアクセスするには、MongoDB Server v 5.0以降に接続し、C ドライバー v 1.18以降を使用します。
この機能を使用すると、Stable API でカバーされるコマンドの下位互換性の問題を心配することなく、ドライバーまたはサーバーをアップデートできます。
注意
2022 年 2 月以降、Versioned API は Stable API という名前に変更しました。この名前の変更に伴うコンセプトと機能の変更はありません。
Stable API なしでの MongoDB Atlas への接続
Stable API をサポートしていないバージョンの MongoDB またはドライバーを使用している場合は、次のコード スニペットを使用して、MongoDB Atlas 上の MongoDB 配置への接続をテストできます。
int main(void) { mongoc_client_t *client = NULL; bson_error_t error = {0}; mongoc_database_t *database = NULL; bson_t *command = NULL; bson_t reply = BSON_INITIALIZER; int rc = 0; bool ok = true; // Initialize the MongoDB C Driver. mongoc_init(); client = mongoc_client_new("<connection string>"); if (!client) { fprintf(stderr, "Failed to create a MongoDB client.\n"); rc = 1; goto cleanup; } // Get a handle on the "admin" database. database = mongoc_client_get_database(client, "admin"); if (!database) { fprintf(stderr, "Failed to get a MongoDB database handle.\n"); rc = 1; goto cleanup; } // Ping the database. command = BCON_NEW("ping", BCON_INT32(1)); ok = mongoc_database_command_simple( database, command, NULL, &reply, &error ); if (!ok) { fprintf(stderr, "error: %s\n", error.message); rc = 1; goto cleanup; } bson_destroy(&reply); printf("Pinged your deployment. You successfully connected to MongoDB!\n"); // Perform cleanup. cleanup: bson_destroy(command); mongoc_database_destroy(database); mongoc_client_destroy(client); mongoc_cleanup(); return rc; }
互換性
Cドライバーの各バージョンと互換性のあるMongoDB ServerおよびC言語のバージョンについては、「 互換性 」ページを参照してください。
ヘルプを受けるには
MongoDB Stack Overflowページ または MongoDB Reddit Community でサポートをリクエストする。