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
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -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
Expand Up @@ -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

Expand Down
134 changes: 134 additions & 0 deletions source/connect-to-mongo/stable-api.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
.. _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. When you update either your driver or server,
the API version changes, which can change the way these operations behave.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you update either your driver or server, the API version changes, which can change the way these operations behave.

This is not accurate. Was this text copied from somewhere else?

https://www.mongodb.com/docs/manual/reference/stable-api/ looks accurate to me, so we should either link to it or crib from it.

Copy link
Collaborator Author

@mcmorisi mcmorisi Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description was taken from the same page from a different driver, yes. Can take this back to the team to investigate whether there's a larger-scope misunderstanding here.

In the meantime, can remove the sentence you called out, as I believe the remaining copy adequately explains the feature (plus we have the link to the server manual).

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
a specified API version, all commands you run with the client use the specified
version. If you must run commands using more than one version of the
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently there is only one version of the Stable API: V1.

Consider changing this to refer to one of the other properties of the ServerApi class, e.g. strict.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we expect another version to release? It could benefit us to cover our bases in advance of future versions.

I can also tweak the last highlighted sentence to the following more general version:

If you must run Stable API commands using alternative configurations, create a new MongoClient.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have heard nothing about a new version coming out.

{+stable-api+}, 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- | **Optional**. When ``True``, if you call a command that isn't part of
- | **Optional**. When ``true``, if you call a command that isn't part of

the declared API version, the driver raises an exception.
|
| Default: **False**
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| Default: **False**
| Default: **false**


* - ``deprecationErrors()``
- | **Optional**. When ``True``, if you call a command that is deprecated in the
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- | **Optional**. When ``True``, if you call a command that is deprecated in the
- | **Optional**. When ``true``, if you call a command that is deprecated in the

declared API version, the driver raises an exception.
|
| Default: **False**
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| Default: **False**
| Default: **false**


The following code example shows how you can set configurations on an instance of ``ServerApi``
by chaining methods on the ``ServerApi.Builder``:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S:

Suggested change
The following code example shows how you can set configurations on an instance of ``ServerApi``
by chaining methods on the ``ServerApi.Builder``:
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 count is not in API Version
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a general operation instead of directly applying to count?

Suggested change
Provided apiStrict:true, but the command count is not in API Version
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
Expand Up @@ -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
--------
Expand Down
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
}
}
Loading