Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ /

MongoDB C ドライバー

公式 MongoDB C ドライバーのドキュメント サイトへようこそ。 アプリケーションにドライバーを追加して、C の MongoDB で動作できます。必要なライブラリ、 libmongoclibbsonをダウンロードするには、 MongoDB C ドライバー ライブラリの取得手順に従ってください。または実行可能なプロジェクトを設定します。チュートリアルに従ってください。

  • Tutorial

  • Usage Guide

  • MongoDB Developer Center

  • API リファレンス

  • 変更履歴

  • ソースコード

  • その他の BSON 例

次の接続スニペットを使用して、Atlas 上の MongoDB 配置への接続をテストできます。

#include <mongoc/mongoc.h>
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 APIStable API という名前に変更しました。この名前の変更に伴うコンセプトと機能の変更はありません。

Stable API をサポートしていないバージョンの MongoDB またはドライバーを使用している場合は、次のコード スニペットを使用して、MongoDB Atlas 上の MongoDB 配置への接続をテストできます。

#include <mongoc/mongoc.h>
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;
}

このセクションの互換性テーブルは、特定のバージョンの MongoDB で使用するための MongoDB C ドライバーの推奨バージョンを示しています。

最初の列は、ドライバーのバージョンの一覧です。

重要

MongoDB は、MongoDB Server のバージョンのサポート終了(EOL)日から 3 年間、MongoDB Server とドライバー間の互換性を保証します。 MongoDB のリリースと EOL の日付の詳細については、「 MongoDB ソフトウェアライフサイクルの予定 」を参照してください。

Icon
説明

すべての機能がサポートされています。

ドライバー バージョンは MongoDB バージョンで動作しますが、すべての新しい MongoDB 機能がサポートされているわけではありません。

ドライバー バージョンは MongoDB バージョンでは動作しません。MongoDB バージョンに接続しようとすると、エラーが発生します。

マークなし

ドライバー バージョンは MongoDB バージョンではテストされていません。

C ドライバーのバージョン
MongoDB 7.0
MongoDB 6.0
MongoDB 5.0
MongoDB 4.4
MongoDB 4.2
MongoDB 4.0
MongoDB 3.6
MongoDB 3.4
MongoDB 3.2
MongoDB 3.0
MongoDB 2.6

1.27

1.26

1.25

1.24

1.23

1.22

1.21

1.20

1.19

1.18

[1]

1.17

1.16

1.15

1.14

1.13

1.12

1.11

1.10

1.9

1.8

1.7

1.6

1.5

1.4

1.3

1.2

1.1

1.0

[1] 1.18ドライバーはセカンダリでのスナップショット読み取りをサポートしていません。 詳細については、 MongoDB Server バージョン5.0リリースノートを参照してください。

ドライバーは MongoDB の古いバージョンをサポートしていません。

次の互換性テーブルは、特定のバージョンの C で使用するための MongoDB C ドライバーの推奨バージョンを示しています。

最初の列は、ドライバーのバージョンの一覧です。

C ドライバーのバージョン
C17/C18
C11
C99
C89

1.27

1.26

1.25

1.24

<= 1.23

互換性テーブルの読み方の詳細については、「MongoDB 互換性テーブル」ガイドを参照してください。

戻る

C

項目一覧