The Versioned API feature requires MongoDB Server 5.0 or later.
You should only use the Versioned API feature if all the MongoDB servers you are connecting to support this feature.
In this guide, you can learn how to specify the Versioned API when connecting to a MongoDB instance or replica set. You can use the Versioned 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 Versioned 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 Versioned API.
See the server manual page on the Versioned API for more information including a list of commands it covers.
The following sections describe how you can enable the Versioned API for your MongoDB client and the options that you can specify.
Enable the Versioned API on a MongoDB Client¶
To enable the Versioned 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 Versioned API.
If you need to run commands using more than one version of the Versioned API, instantiate a separate client with that version.
If you need to run commands not covered by the Versioned API, make sure the "strict" option is disabled. See the section on Versioned API Options for more information.
The following example shows how you can instantiate a
sets the Versioned API version and connects to a server by performing the
- Construct a
ServerApiinstance using the
- Specify a Versioned API version using a constant from the
- Construct a
MongoClientSettingsinstance using the
- Specify a server to connect to using a
- Instantiate 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 Versioned 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:
Versioned API Options¶
You can enable or disable optional behavior related to the Versioned 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: