|
| 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>`__ |
0 commit comments