Skip to content

DOCSP-42298: Stable API #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion snooty.toml
Original file line number Diff line number Diff line change
@@ -27,4 +27,5 @@ java-rs = "Java Reactive Streams"
string-data-type = "``String``"
bool = "``boolean``"
pr = "Project Reactor"
mdb-server = "MongoDB Server"
mdb-server = "MongoDB Server"
stable-api = "Stable API"
6 changes: 3 additions & 3 deletions source/connect-to-mongo/choose-connection-target.txt
Original file line number Diff line number Diff line change
@@ -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

131 changes: 131 additions & 0 deletions source/connect-to-mongo/stable-api.txt
Original file line number Diff line number Diff line change
@@ -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>`__
1 change: 1 addition & 0 deletions source/connect.txt
Original file line number Diff line number Diff line change
@@ -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
--------
38 changes: 38 additions & 0 deletions source/includes/connect/stable-api.java
Original file line number Diff line number Diff line change
@@ -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
}
}