AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
MongoDB Branding Shape
Click here >
Docs Menu

Stable API

注意

Stable API 機能には MongoDB Server 5.0 以降が必要です。

このガイドでは、MongoDB 配置に接続するときにStable API互換性を指定する方法を学習できます。

Stable API 機能を使用すると、指定された API バージョンと互換性のある動作でサーバーに操作が強制的に実行されます。 ドライバーまたはサーバーのいずれかを更新すると、API バージョンが変更され、これらの操作の動作が変わる可能性があります。 Stable API を使用すると、サーバーからの一貫した応答が確保され、アプリケーションの API の長期的な安定性が確保されます。

次のセクションでは、MongoDB クライアントの Stable API を有効にしてカスタマイズする方法について説明します。 Stable APIサポートするコマンドのリストを含むStable API の詳細については、MongoDB Server マニュアルの「 」を参照してください。

Stable API を有効にするには、次の手順を実行します。

  1. mongocxx::options::server_apiオブジェクトを構築し、Stable APIバージョンを指定します。 mongocxx::options::server_api::version列挙型 で定義された Stable APIバージョンを使用する必要があります。 現在、ドライバーはバージョン1 ( k_version_1 )のみをサポートしています。

  2. mongocxx::options::clientオブジェクトを構築します。 このオブジェクトのserver_api_optsフィールドを、前のステップで作成したserver_apiオブジェクトに設定します。

  3. mongocxx::uriオブジェクトと前のステップで作成したmongocxx::options::clientオブジェクトを渡して、 mongocxx::clientオブジェクトを構築します。

次のコード例は、Stable API バージョン1を指定する方法を示しています。

#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/client.hpp>
int main()
{
mongocxx::instance instance;
mongocxx::uri uri("mongodb://<hostname>:<port>");
mongocxx::options::server_api server_api_options(mongocxx::options::server_api::version::k_version_1);
mongocxx::options::client client_options;
client_options.server_api_opts(server_api_options);
mongocxx::client client(uri, client_options);
}

注意

指定されたAPIバージョンでmongocxx::clientインスタンスを作成すると、クライアントで実行されるすべてのコマンドで指定されたバージョンが使用されます。 Stable APIの複数のバージョンを使用してコマンドを実行する必要がある場合は、新しいmongocxx::clientインスタンスを作成します。

次の表では、 server_api_optionsクラスのプロパティを説明します。 これらのプロパティを使用して、 Stable APIの動作をカスタマイズできます。

オプション名
説明

厳密

true任意。

の場合、宣言したAPIバージョンに含まれないコマンドを呼び出すと、ドライバーは例外を発生させます。デフォルト:false

delete_errors

true任意。

の場合、宣言したAPIバージョンで非推奨のコマンドを呼び出すと、ドライバーは例外を発生させます。デフォルト:false

次のコード例は、 ServerApiオブジェクトを構築するときにこれらのパラメータを使用する方法を示しています。

#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/client.hpp>
int main()
{
mongocxx::instance instance;
mongocxx::uri uri("mongodb://<hostname>:<port>");
mongocxx::options::server_api server_api_options(mongocxx::options::server_api::version::k_version_1);
server_api_options.strict(true);
server_api_options.deprecation_errors(true);
mongocxx::options::client client_options;
client_options.server_api_opts(server_api_options);
mongocxx::client client(uri, client_options);
}

C++ドライバーで Stable APIを使用する方法の詳細については、次のAPIドキュメントを参照してください。