diff --git a/snooty.toml b/snooty.toml index 886aefbe..3d936f31 100644 --- a/snooty.toml +++ b/snooty.toml @@ -27,4 +27,5 @@ java-rs = "Java Reactive Streams" string-data-type = "``String``" bool = "``boolean``" pr = "Project Reactor" -mdb-server = "MongoDB Server" \ No newline at end of file +mdb-server = "MongoDB Server" +stable-api = "Stable API" \ No newline at end of file diff --git a/source/connect-to-mongo/choose-connection-target.txt b/source/connect-to-mongo/choose-connection-target.txt index 5bfac146..3758a506 100644 --- a/source/connect-to-mongo/choose-connection-target.txt +++ b/source/connect-to-mongo/choose-connection-target.txt @@ -40,13 +40,13 @@ Then, pass your connection string to the ``create()`` method constructing a ``Mo Follow the :atlas:`Atlas driver connection guide </driver-connection>` to retrieve your connection string. -When you connect to Atlas, we recommend using the Stable API client option to avoid +When you connect to Atlas, we recommend using the {+stable-api+} client option to avoid breaking changes when Atlas upgrades to a new version of MongoDB Server. -To learn more about the Stable API feature, see :manual:`Stable API +To learn more about the {+stable-api+} feature, see :manual:`Stable API </reference/stable-api>` in the MongoDB Server manual. The following code shows how to use the {+driver-short+} to connect to an Atlas cluster. The -code uses the ``serverApi`` option to specify a Stable API version. +code uses the ``serverApi`` option to specify a {+stable-api+} version. .. include:: /includes/connect/connect-reactor.rst diff --git a/source/connect-to-mongo/stable-api.txt b/source/connect-to-mongo/stable-api.txt new file mode 100644 index 00000000..eda7f268 --- /dev/null +++ b/source/connect-to-mongo/stable-api.txt @@ -0,0 +1,131 @@ +.. _java-rs-stable-api: + +========== +{+stable-api+} +========== + +.. contents:: On this page + :local: + :backlinks: none + :depth: 2 + :class: singlecol + +.. facet:: + :name: genre + :values: reference + +.. meta:: + :keywords: compatible, backwards, upgrade + +.. note:: + + The {+stable-api+} feature requires {+mdb-server+} 5.0 or later. + +Overview +-------- + +In this guide, you can learn how to specify **{+stable-api+}** compatibility when +connecting to a MongoDB deployment. + +The {+stable-api+} feature forces the server to run operations with behaviors compatible +with the API version you specify. Using the {+stable-api+} ensures consistent responses +from the server and provides long-term API stability for your application. + +The following sections describe how you can enable and customize {+stable-api+} for +your MongoDB client. For more information about the {+stable-api+}, including a list of +the commands it supports, see :manual:`Stable API </reference/stable-api/>` in the +{+mdb-server+} manual. + +Enable the {+stable-api+} +------------------------- + +To enable the {+stable-api+}, perform the following steps: + +1. Construct a ``ServerApi`` object and specify a {+stable-api+} version. You must use + a {+stable-api+} version defined in the ``ServerApiVersion`` enum. +#. Construct a ``MongoClientSettings`` object using the ``MongoClientSettings.Builder`` class. +#. Instantiate a ``MongoClient`` using the ``MongoClients.create()`` method and + pass your ``MongoClientSettings`` instance as a parameter. + +The following code example shows how to specify {+stable-api+} version 1: + +.. literalinclude:: /includes/connect/stable-api.java + :start-after: start-enable-stable-api + :end-before: end-enable-stable-api + :language: java + :copyable: + :dedent: + +Once you create a ``MongoClient`` instance with the {+stable-api+}, all commands you +run with the client use the specified {+stable-api+} configuration. If you must run +commands using alternative configurations, create a new ``MongoClient``. + +.. _java-rs-stable-api-options: + +Configure the {+stable-api+} +---------------------------- + +The following table describes chainable methods of the ``ServerApi.Builder`` class that +you can use to customize the behavior of the {+stable-api+}. + +.. list-table:: + :header-rows: 1 + :stub-columns: 1 + :widths: 25,75 + + * - Option Name + - Description + + * - ``strict()`` + - | **Optional**. When ``true``, if you call a command that isn't part of + the declared API version, the driver raises an exception. + | + | Default: **false** + + * - ``deprecationErrors()`` + - | **Optional**. When ``true``, if you call a command that is deprecated in the + declared API version, the driver raises an exception. + | + | Default: **false** + +The following code example shows how you can configure an instance of ``ServerApi`` +by chaining methods on the ``ServerApi.Builder``: + +.. literalinclude:: /includes/connect/stable-api.java + :start-after: start-stable-api-options + :end-before: end-stable-api-options + :language: java + :copyable: + :dedent: + +Troubleshooting +--------------- + +Unrecognized field 'apiVersion' on server +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The {+driver-short+} raises this exception if you specify an API version and connect to a +MongoDB server that doesn't support the {+stable-api+}. Ensure you're connecting to a +deployment running {+mdb-server+} v5.0 or later. + +Provided apiStrict:true, but the command <operation> is not in API Version +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The {+driver-short+} raises this exception if your ``MongoClient`` runs an operation that +isn't in the {+stable-api+} version you specified. To avoid this error, use an alternative +operation supported by the specified {+stable-api+} version, or set the ``strict`` +option to ``False`` when constructing your ``ServerApi`` object. + +API Documentation +----------------- + +For more information about using the {+stable-api+} with the {+driver-short+}, see the +following API documentation: + +- `ServerApi <{+api+}/mongodb-driver-core/com/mongodb/ServerApi.html>`__ +- `ServerApi.Builder <{+api+}/mongodb-driver-core/com/mongodb/ServerApi.Builder.html>`__ +- `ServerApiVersion <{+api+}/mongodb-driver-core/com/mongodb/ServerApiVersion.html>`__ +- `ServerAddress <{+api+}/mongodb-driver-core/com/mongodb/ServerAddress.html>`__ +- `MongoClientSettings <{+api+}/mongodb-driver-core/com/mongodb/MongoClientSettings.html>`__ +- `MongoClientSettings.Builder <{+api+}/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html>`__ +- `MongoClients <{+api+}/mongodb-driver-reactivestreams/com/mongodb/reactivestreams/client/MongoClients.html>`__ diff --git a/source/connect.txt b/source/connect.txt index ffa419b1..75cdaf59 100644 --- a/source/connect.txt +++ b/source/connect.txt @@ -27,6 +27,7 @@ Connect to MongoDB /connect-to-mongo/connection-options/ /connect-to-mongo/tls/ /connect-to-mongo/compression/ + /connect-to-mongo/stable-api/ Overview -------- diff --git a/source/includes/connect/stable-api.java b/source/includes/connect/stable-api.java new file mode 100644 index 00000000..5cb32717 --- /dev/null +++ b/source/includes/connect/stable-api.java @@ -0,0 +1,38 @@ +package org.example; + +import com.mongodb.ConnectionString; +import com.mongodb.MongoClientSettings; +import com.mongodb.ServerApi; +import com.mongodb.ServerApiVersion; + +import com.mongodb.reactivestreams.client.*; + +public class Main { + public static void main(String[] args) { + // start-enable-stable-api + ServerApi serverApi = ServerApi.builder() + .version(ServerApiVersion.V1) + .build(); + + // Replace the placeholder with your Atlas connection string + String uri = "<connection string URI>"; + + MongoClientSettings settings = MongoClientSettings.builder() + .applyConnectionString(new ConnectionString(uri)) + .serverApi(serverApi) + .build(); + + try (MongoClient mongoClient = MongoClients.create(settings)) { + // Perform client operations here + } + // end-enable-stable-api + + // start-stable-api-options + ServerApi serverApi = ServerApi.builder() + .version(ServerApiVersion.V1) + .strict(true) + .deprecationErrors(true) + .build(); + // end-stable-api-options + } +} \ No newline at end of file