|
| 1 | +============= |
| 2 | +Versioned API |
| 3 | +============= |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Declaring an API version |
| 14 | +------------------------ |
| 15 | + |
| 16 | +To declare an API version, pass a ``serverApi`` driver option when creating your |
| 17 | +client. The value is a |
| 18 | +:php:`MongoDB\\Driver\\ServerApi <class.mongodb-driver-serverapi>` instance that |
| 19 | +contains API version information. This feature is introduced in MongoDB 5.0, |
| 20 | +which will initially support only API version "1". Additional versions may be |
| 21 | +introduced in future versions of the server. |
| 22 | + |
| 23 | +.. code-block:: php |
| 24 | + |
| 25 | + <?php |
| 26 | + |
| 27 | + use MongoDB\Client; |
| 28 | + use MongoDB\Driver\ServerApi; |
| 29 | + |
| 30 | + $serverApi = new ServerApi(ServerApi::V1); |
| 31 | + $client = new Client('mongodb://127.0.0.1', [], ['serverApi' => $serverApi]); |
| 32 | + |
| 33 | + // Command includes the declared API version |
| 34 | + $client->database->collection->find([]); |
| 35 | + |
| 36 | +.. note:: |
| 37 | + |
| 38 | + Only declare an API version when connecting to a deployment that has no |
| 39 | + pre-5.0 members. Older servers will error when encountering commands with a |
| 40 | + declared API version. |
| 41 | + |
| 42 | +Strict API |
| 43 | +---------- |
| 44 | + |
| 45 | +By default, declaring an API version guarantees behavior for commands that are |
| 46 | +part of the versioned API, but does not forbid using commands that are not part |
| 47 | +of the API version. To only allow commands and options that are part of the |
| 48 | +versioned API, specify the ``strict`` option when creating the |
| 49 | +specify the ``strict`` option when creating the |
| 50 | +:php:`MongoDB\\Driver\\ServerApi <class.mongodb-driver-serverapi>` instance: |
| 51 | + |
| 52 | +.. code-block:: php |
| 53 | + |
| 54 | + <?php |
| 55 | + |
| 56 | + use MongoDB\Client; |
| 57 | + use MongoDB\Driver\ServerApi; |
| 58 | + |
| 59 | + $serverApi = new ServerApi(ServerApi::V1, true); |
| 60 | + $client = new Client('mongodb://127.0.0.1', [], ['serverApi' => $serverApi]); |
| 61 | + |
| 62 | + // Will fail as the tailable option is not supported in versioned API |
| 63 | + $client->database->collection->find([], ['tailable' => true]); |
| 64 | + |
| 65 | +Fail on Deprecated Commands |
| 66 | +--------------------------- |
| 67 | + |
| 68 | +The optional ``deprecationErrors`` option causes MongoDB to fail all commands |
| 69 | +or behaviors that have been deprecated in the API version. This can be used in |
| 70 | +testing to ensure a smooth transition to a future API version. |
| 71 | + |
| 72 | +.. code-block:: php |
| 73 | + |
| 74 | + <?php |
| 75 | + |
| 76 | + use MongoDB\Client; |
| 77 | + use MongoDB\Driver\ServerApi; |
| 78 | + |
| 79 | + $serverApi = new ServerApi(ServerApi::V1, null, true); |
| 80 | + $client = new Client('mongodb://127.0.0.1', [], ['serverApi' => $serverApi]); |
| 81 | + |
| 82 | +.. note:: |
| 83 | + |
| 84 | + At the time of this writing, no part of API version "1" has been deprecated. |
| 85 | + |
| 86 | +Usage with the command helper |
| 87 | +----------------------------- |
| 88 | + |
| 89 | +When using the :phpmethod:`MongoDB\\Database::command()` method to run arbitrary |
| 90 | +commands, the API version declared to the client is automatically appended to |
| 91 | +the command document. Setting any of the ``apiVersion``, ``apiStrict``, or |
| 92 | +``apiDeprecationErrors`` command options in the command document and calling |
| 93 | +:phpmethod:`MongoDB\\Database::command()` from a client with a declared API |
| 94 | +version is not supported and will lead to undefined behavior. |
0 commit comments