The Stable API feature requires MongoDB Server 5.0 or later.
You should only use the Stable API feature if all the MongoDB servers you are connecting to support this feature.
In this guide, you can learn how to specify the Stable API when connecting to a MongoDB instance or replica set. You can use the Stable API feature to force the server to run operations with behavior compatible with the specified API version. An API version defines the expected behavior of the operations it covers and the format of server responses. If you change to a different API version, the operations are not guaranteed to be compatible and the server responses are not guaranteed to be similar.
When you use the Stable API feature with an official MongoDB driver, you can update your driver or server without worrying about backward compatibility issues of the commands covered by the Stable API.
See the MongoDB reference page on the Stable API for more information including a list of commands it covers.
The following sections describe how you can enable the Stable API for your MongoDB client and the options that you can specify.
To enable the Stable API, you must specify an API version in the settings
of your MongoDB client. Once you instantiate a
MongoClient instance with
a specified API version, all commands you run with that client use that
version of the Stable API.
If you need to run commands using more than one version of the Stable API, instantiate a separate client with that version.
If you need to run commands not covered by the Stable API, make sure the "strict" option is disabled. See the section on Stable API Options for more information.
The following example shows how you can instantiate a
sets the Stable API version and connects to a server by performing the
ServerApiinstance using the
Specify a Stable API version using a constant from the
MongoClientSettingsinstance using the
Specify a server to connect to using a
MongoClients.create()method and pass your
MongoClientSettingsinstance as a parameter.
ServerApi serverApi = ServerApi.builder() .version(ServerApiVersion.V1) .build(); // Replace the uri string with your MongoDB deployment's connection string String uri = "<connection string uri>"; MongoClientSettings settings = MongoClientSettings.builder() .applyConnectionString(new ConnectionString(uri)) .serverApi(serverApi) .build(); MongoClient client = MongoClients.create(settings);
If you specify an API version and connect to a MongoDB server that does
not support the Stable API, your application may raise an exception when
executing a command on your MongoDB server. If you use a
that specifies the API version to query a server that does not support it,
your query could fail with an exception message that includes the
'Unrecognized field 'apiVersion' on server...
For more information on the methods and classes referenced in this section, see the following API Documentation:
You can enable or disable optional behavior related to the Stable API as described in the following table.
Optional. When set, if you call a command that is not part of the declared API version, the driver raises an exception.
Optional. When set, if you call a command that is deprecated in the declared API version, the driver raises an exception.
The following example shows how you can set the two options on an instance
ServerApi by chaining methods on the
ServerApi serverApi = ServerApi.builder() .version(ServerApiVersion.V1) .strict(true) .deprecationErrors(true) .build();
For more information on the options in this section, see the following API Documentation: