|
| 1 | +.. _rust-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 | +.. important:: |
| 14 | + |
| 15 | + To use the {+stable-api+} feature, you must connect to a deployment running MongoDB Server version 5.0 or later. |
| 16 | + |
| 17 | + You should only use the {+stable-api+} feature if all the MongoDB |
| 18 | + servers you are connecting to support this feature. |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to specify **{+stable-api+}** |
| 24 | +compatibility when connecting to a MongoDB instance or replica set. |
| 25 | + |
| 26 | +The {+stable-api+} feature forces the server to run operations with |
| 27 | +behaviors that are compatible with the **API version** you specify. An API |
| 28 | +version defines the expected behavior of the operations it covers and |
| 29 | +the format of server responses. The operations and the server responses |
| 30 | +may differ depending on the API version you specify. |
| 31 | + |
| 32 | +When you use the {+stable-api+} feature with an official MongoDB driver, you |
| 33 | +can update your driver or server without worrying about backward |
| 34 | +compatibility issues of the commands covered by the {+stable-api+}. |
| 35 | + |
| 36 | +To learn more about the commands that the server covers, see |
| 37 | +:manual:`{+stable-api+} </reference/stable-api/>` in the Server manual. |
| 38 | + |
| 39 | +Specify an API Version |
| 40 | +---------------------- |
| 41 | + |
| 42 | +To specify an API version, define a ``ServerApi`` struct and set the ``server_api`` field |
| 43 | +of your ``ClientOptions`` instance to this struct. |
| 44 | +The ``ServerApi`` struct contains your server API version and options. |
| 45 | +To learn more about the options, see the :ref:`Modify Behavior <rust-stable-api-options>` section of this guide. |
| 46 | + |
| 47 | +After you specify an API version, the client runs operations that are compatible with the API version. |
| 48 | + |
| 49 | +.. note:: |
| 50 | + |
| 51 | + The {+driver-long+} currently supports only {+stable-api+} version 1. |
| 52 | + |
| 53 | +API Version Specification Example |
| 54 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 55 | + |
| 56 | +The following example sets the {+stable-api+} version when instantiating a ``Client`` |
| 57 | +and connects to a server. |
| 58 | + |
| 59 | +.. literalinclude:: /includes/fundamentals/code-snippets/stable-api.rs |
| 60 | + :language: rust |
| 61 | + :dedent: |
| 62 | + :start-after: start-stable-api |
| 63 | + :end-before: end-stable-api |
| 64 | + |
| 65 | +.. _rust-stable-api-options: |
| 66 | + |
| 67 | +Modify Behavior |
| 68 | +--------------- |
| 69 | + |
| 70 | +You can modify the behavior of the {+stable-api+} feature by setting fields in the ``ServerApi`` struct. |
| 71 | + |
| 72 | +While you can set the fields in a ``ServerApi`` struct manually, you can use the builder design pattern to define the struct more efficiently. |
| 73 | + |
| 74 | +.. list-table:: |
| 75 | + :header-rows: 1 |
| 76 | + :stub-columns: 1 |
| 77 | + :class: compatibility-large |
| 78 | + |
| 79 | + * - Field |
| 80 | + - Description |
| 81 | + |
| 82 | + * - ``version`` |
| 83 | + - | The {+stable-api+} version to use. |
| 84 | + | Specified with the ``ServerAPIVersion`` enum. |
| 85 | + |
| 86 | + | Type: ``ServerAPIVersion`` |
| 87 | + | Default: ``ServerAPIVersion1`` |
| 88 | + |
| 89 | + * - ``strict`` |
| 90 | + - | Indicates whether the server should return errors for features that aren't part of the API version. |
| 91 | + | |
| 92 | + | Type: ``bool`` |
| 93 | + | Default: ``false`` |
| 94 | + |
| 95 | + * - ``deprecation_errors`` |
| 96 | + - | Indicates whether the server should return errors for deprecated features. |
| 97 | + | |
| 98 | + | Type: ``bool`` |
| 99 | + | Default: ``false`` |
| 100 | + |
| 101 | +.. _rust-stable-api-options-example: |
| 102 | + |
| 103 | +Stable API Options Example |
| 104 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 105 | + |
| 106 | +This example enables the {+stable-api+} feature with the following specifications: |
| 107 | + |
| 108 | +- Uses {+stable-api+} version 1 |
| 109 | +- Returns errors for features that aren't part of version 1 |
| 110 | +- Returns errors for deprecated features |
| 111 | + |
| 112 | +.. literalinclude:: /includes/fundamentals/code-snippets/stable-api-behavior.rs |
| 113 | + :language: rust |
| 114 | + :dedent: |
| 115 | + :start-after: start-stable-api-behavior |
| 116 | + :end-before: end-stable-api-behavior |
| 117 | + |
| 118 | +Additional Information |
| 119 | +---------------------- |
| 120 | + |
| 121 | +To learn more about connecting to your MongoDB instance or replica set, |
| 122 | +see the :ref:`rust-connect-to-mongodb`. |
| 123 | + |
| 124 | +API Documentation |
| 125 | +~~~~~~~~~~~~~~~~~ |
| 126 | + |
| 127 | +To learn more about any of the methods or types discussed in this |
| 128 | +guide, see the following API Documentation: |
| 129 | + |
| 130 | +- `Client <{+api+}/struct.Client.html>`__ |
| 131 | +- `ClientOptions <{+api+}/options/struct.ClientOptions.html>`__ |
| 132 | +- `ServerAPI <{+api+}/options/struct.ServerApi.html>`__ |
| 133 | +- `ServerApiVersion <{+api+}/options/enum.ServerApiVersion.html>`__ |
| 134 | +- `with_options() <{+api+}/options/struct.Client.html#method.with_options>`__ |
| 135 | + |
0 commit comments