Skip to content

Commit f31eb3e

Browse files
mongoKartjmikola
andauthored
DOCSP-41962 - Stable API (#117)
Co-authored-by: Jeremy Mikola <jmikola@gmail.com>
1 parent f0e562c commit f31eb3e

File tree

3 files changed

+141
-2
lines changed

3 files changed

+141
-2
lines changed

snooty.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ php-library = "MongoDB PHP Library"
3636

3737
[constants]
3838
php-library = "MongoDB PHP Library"
39-
library-short = "PHP library"
40-
stable-api = "Stable API"
39+
driver-short = "PHP library"
40+
extension-short = "PHP extension"
4141
mdb-server = "MongoDB Server"
42+
stable-api = "Stable API"
43+
library-short = "PHP library"
4244
api = "https://www.mongodb.com/docs/php-library/current/reference"
4345
php-manual = "https://www.php.net/manual/en"

source/connect/stable-api.txt

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
.. _php-stable-api:
2+
3+
==============
4+
{+stable-api+}
5+
==============
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: compatible, backwards, upgrade
19+
20+
.. note::
21+
22+
The {+stable-api+} feature requires {+mdb-server+} 5.0 or later.
23+
24+
Overview
25+
--------
26+
27+
In this guide, you can learn how to specify **{+stable-api+}** compatibility when
28+
connecting to a MongoDB deployment.
29+
30+
The {+stable-api+} feature forces the server to run operations with behaviors compatible
31+
with the API version you specify. When you update either your library or server version,
32+
the API version changes, which can change the way these operations behave.
33+
Using the {+stable-api+} ensures consistent responses from the server and
34+
provides long-term API stability for your application.
35+
36+
The following sections describe how you can enable and customize {+stable-api+} for
37+
your MongoDB client. For more information about the {+stable-api+}, including a list of
38+
the commands it supports, see :manual:`Stable API </reference/stable-api/>` in the
39+
{+mdb-server+} manual.
40+
41+
Enable the {+stable-api+}
42+
-------------------------
43+
44+
To enable the {+stable-api+}, perform the following steps:
45+
46+
1. Construct a ``MongoDB\Driver\ServerApi`` object and pass the {+stable-api+}
47+
version you want to use. Currently, the library supports only version 1.
48+
#. Construct a ``MongoDB\Client`` object. For the ``driverOptions`` parameter, pass an
49+
array that contains the ``serverApi`` option. Set this option to the
50+
``MongoDB\Driver\ServerApi`` object you created in the previous step.
51+
52+
The following code example shows how to specify {+stable-api+} version 1:
53+
54+
.. literalinclude:: /includes/connect/stable-api.php
55+
:language: php
56+
:copyable: true
57+
:start-after: // start-specify-v1
58+
:end-before: // end-specify-v1
59+
:emphasize-lines: 3-4
60+
61+
.. note::
62+
63+
After you create a ``MongoDB\Client`` instance with
64+
a specified API version, all commands you run with the client use the specified
65+
version. If you need to run commands using more than one version of the
66+
{+stable-api+}, create a new ``MongoDB\Client`` instance.
67+
68+
.. _stable-api-options:
69+
70+
Configure the {+stable-api+}
71+
------------------------
72+
73+
The ``MongoDB\Driver\ServerApi`` constructor also accepts the following optional parameters.
74+
You can use these parameters to customize the behavior of the {+stable-api+}.
75+
76+
.. list-table::
77+
:header-rows: 1
78+
:stub-columns: 1
79+
:widths: 25,75
80+
81+
* - Parameter
82+
- Description
83+
84+
* - strict
85+
- | **Optional**. When ``true``, if you call a command that isn't part of
86+
the declared API version, the server raises an exception.
87+
|
88+
| Default: ``null``. If this parameter is null, the server applies its default
89+
value of ``false``.
90+
91+
* - deprecationErrors
92+
- | **Optional**. When ``true``, if you call a command that is deprecated in the
93+
declared API version, the server raises an exception.
94+
|
95+
| Default: ``null``. If this parameter is null, the server applies its default
96+
value of ``false``.
97+
98+
The following code example shows how you can use these parameters when constructing a
99+
``MongoDB\Driver\ServerApi`` object:
100+
101+
.. literalinclude:: /includes/connect/stable-api.php
102+
:language: php
103+
:copyable: true
104+
:start-after: // start-stable-api-options
105+
:end-before: // end-stable-api-options
106+
:emphasize-lines: 3-4
107+
108+
API Documentation
109+
-----------------
110+
111+
For more information about the ``MongoDB\Client`` class, see the following {+driver-short+}
112+
API documentation:
113+
114+
- :phpclass:`MongoDB\Client`
115+
116+
For more information about the ``MongoDB\Driver\ServerApi`` class, see the following
117+
{+extension-short+} API documentation:
118+
119+
- `MongoDB\\Driver\\ServerApi <https://www.php.net/manual/en/class.mongodb-driver-serverapi.php>`__
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
// start-specify-v1
4+
$uri = "mongodb://<hostname>:<port>";
5+
6+
$driverOptions = ['serverApi' => new MongoDB\Driver\ServerApi('1')];
7+
8+
$client = new MongoDB\Client($uri, [], $driverOptions);
9+
// end-specify-v1
10+
11+
// start-stable-api-options
12+
$uri = "mongodb://<hostname>:<port>";
13+
14+
$serverApi = new MongoDB\Driver\ServerApi('1', strict: true, deprecationErrors: true);
15+
$driverOptions = ['serverApi' => $serverApi];
16+
17+
$client = new MongoDB\Client($uri, [], $driverOptions);
18+
// end-stable-api-options

0 commit comments

Comments
 (0)