@@ -12,7 +12,7 @@ Versioned API
12
12
13
13
What is the Versioned API, and Should You Use It?
14
14
-------------------------------------------------
15
-
15
+
16
16
The MongoDB Versioned API lets you upgrade your MongoDB server at will,
17
17
and ensure that behavior changes between MongoDB versions do not break
18
18
your application.
@@ -44,36 +44,168 @@ resulting from server upgrades. This guarantee holds as long as the
44
44
new server supports your specified API version.
45
45
46
46
To guarantee backward compatibility, your application must:
47
-
47
+
48
48
- Declare an API version
49
49
- Only use commands and features supported in your specified API version
50
50
- Deploy with a supported version of an official driver
51
51
52
52
Declare the API Version
53
53
-----------------------
54
54
55
+ ----------
56
+
57
+ .. |arrow| unicode:: U+27A4
58
+
59
+ |arrow| Use the **Select your language** drop-down menu in the
60
+ upper-right to set the language of the examples on this page.
61
+
62
+ ----------
63
+
55
64
To use the Versioned API, upgrade to the latest driver and create your
56
65
application's MongoClient:
57
66
58
- .. code-block:: none
67
+ .. tabs-selector:: drivers
68
+
69
+ .. tabs-drivers::
70
+
71
+ .. tab::
72
+ :tabid: shell
59
73
60
- client = MongoClient(
61
- <connection string>,
62
- api={"version": "1", "strict": True}
63
- )
74
+ .. code-block:: javascript
75
+
76
+ mongosh --apiVersion 1
64
77
65
- Alternatively, if you are using :mongosh:`MongoDB Shell </>`,
66
- you can specify the :option:`--apiStrict` and :option:`--apiVersion`
67
- connection options:
78
+ .. tab::
79
+ :tabid: python
68
80
69
- .. code-block:: shell
81
+ .. literalinclude:: /driver-examples/test_examples.py
82
+ :language: python
83
+ :dedent: 8
84
+ :start-after: Start Versioned API Example 1
85
+ :end-before: End Versioned API Example 1
70
86
71
- mongosh --apiVersion 1 --apiStrict
87
+ .. tab::
88
+ :tabid: php
89
+
90
+ .. literalinclude:: /driver-examples/DocumentationExamplesTest.php
91
+ :language: php
92
+ :dedent: 8
93
+ :start-after: Start Versioned API Example 1
94
+ :end-before: End Versioned API Example 1
95
+
96
+ .. tab::
97
+ :tabid: c
98
+
99
+ .. literalinclude:: /driver-examples/test-mongoc-sample-commands.c
100
+ :language: c
101
+ :dedent: 3
102
+ :start-after: Start Versioned API Example 1
103
+ :end-before: End Versioned API Example 1
104
+
105
+ .. tab::
106
+ :tabid: go
107
+
108
+ .. literalinclude:: /driver-examples/go_examples.go
109
+ :language: go
110
+ :dedent: 0
111
+ :start-after: Start Versioned API Example 1
112
+ :end-before: End Versioned API Example 1
113
+
114
+ .. tab::
115
+ :tabid: csharp
116
+
117
+ .. literalinclude:: /driver-examples/VersionedApiExamples.cs
118
+ :language: csharp
119
+ :dedent: 12
120
+ :start-after: Start Versioned API Example 1
121
+ :end-before: End Versioned API Example 1
122
+
123
+ .. tab::
124
+ :tabid: nodejs
125
+
126
+ .. literalinclude:: /driver-examples/node_versioned_api.js
127
+ :language: nodejs
128
+ :dedent: 4
129
+ :start-after: Start Versioned API Example 1
130
+ :end-before: End Versioned API Example 1
72
131
73
132
``"1"`` is currently the only API version available.
74
133
75
- Setting ``{ "strict" : True }`` or :option:`--apiStrict` means the
76
- server will reject all commands outside of the Versioned API.
134
+ By default, clients are *non-strict*. A non-strict client allows you
135
+ to run any command, regardless of whether or not it belongs to the
136
+ Versioned API.
137
+
138
+ Create a Strict Client
139
+ ----------------------
140
+
141
+ A *strict* client rejects all commands outside of the Versioned API.
142
+ Attempts to use commands outside of the Versioned API will receive the
143
+ :ref:`APIVersionError <api-vers-resp>` response.
144
+
145
+ Use the sample code to create a *strict* client:
146
+
147
+ .. tabs-drivers::
148
+
149
+ .. tab::
150
+ :tabid: shell
151
+
152
+ .. code-block:: javascript
153
+
154
+ mongosh --apiVersion 1 --apiStrict
155
+
156
+ .. tab::
157
+ :tabid: python
158
+
159
+ .. literalinclude:: /driver-examples/test_examples.py
160
+ :language: python
161
+ :dedent: 8
162
+ :start-after: Start Versioned API Example 2
163
+ :end-before: End Versioned API Example 2
164
+
165
+ .. tab::
166
+ :tabid: php
167
+
168
+ .. literalinclude:: /driver-examples/DocumentationExamplesTest.php
169
+ :language: php
170
+ :dedent: 8
171
+ :start-after: Start Versioned API Example 2
172
+ :end-before: End Versioned API Example 2
173
+
174
+ .. tab::
175
+ :tabid: c
176
+
177
+ .. literalinclude:: /driver-examples/test-mongoc-sample-commands.c
178
+ :language: c
179
+ :dedent: 3
180
+ :start-after: Start Versioned API Example 2
181
+ :end-before: End Versioned API Example 2
182
+
183
+ .. tab::
184
+ :tabid: go
185
+
186
+ .. literalinclude:: /driver-examples/go_examples.go
187
+ :language: go
188
+ :dedent: 0
189
+ :start-after: Start Versioned API Example 2
190
+ :end-before: End Versioned API Example 2
191
+
192
+ .. tab::
193
+ :tabid: csharp
194
+
195
+ .. literalinclude:: /driver-examples/VersionedApiExamples.cs
196
+ :language: csharp
197
+ :dedent: 12
198
+ :start-after: Start Versioned API Example 2
199
+ :end-before: End Versioned API Example 2
200
+
201
+ .. tab::
202
+ :tabid: nodejs
203
+
204
+ .. literalinclude:: /driver-examples/node_versioned_api.js
205
+ :language: nodejs
206
+ :dedent: 4
207
+ :start-after: Start Versioned API Example 2
208
+ :end-before: End Versioned API Example 2
77
209
78
210
Migrate To Versioned API Commands
79
211
---------------------------------
@@ -135,7 +267,7 @@ However, the :dbcommand:`aggregate` command is
135
267
obtain a count in the following way:
136
268
137
269
.. code-block:: javascript
138
-
270
+
139
271
db.sales.aggregate([
140
272
{
141
273
$group: {
@@ -157,23 +289,72 @@ How To Use Commands and Features Outside of the Versioned API
157
289
158
290
To use commands and features outside of the Versioned API, you can
159
291
connect to your deployment with a *non-strict* client. By default,
160
- clients are *non-strict* unless specified otherwise in the connection.
292
+ clients are *non-strict*.
293
+
294
+ Use the sample code to create a *non-strict* client:
295
+
296
+ .. tabs-drivers::
297
+
298
+ .. tab::
299
+ :tabid: shell
161
300
162
- .. code-block:: none
301
+ .. code-block:: javascript
302
+
303
+ mongosh --apiVersion 1
163
304
164
- # Non-strict client
165
- client = MongoClient(
166
- <connection string>,
167
- api={"version": "1", "strict": False}
168
- )
305
+ .. tab::
306
+ :tabid: python
169
307
170
- If you are running :mongosh:`mongosh </>`, you can create a
171
- non-strict client by specifying :option:`--apiVersion` and omitting
172
- the :option:`--apiStrict` option.
308
+ .. literalinclude:: /driver-examples/test_examples.py
309
+ :language: python
310
+ :dedent: 8
311
+ :start-after: Start Versioned API Example 3
312
+ :end-before: End Versioned API Example 3
173
313
174
- .. code-block:: shell
314
+ .. tab::
315
+ :tabid: php
175
316
176
- mongosh --apiVersion 1
317
+ .. literalinclude:: /driver-examples/DocumentationExamplesTest.php
318
+ :language: php
319
+ :dedent: 8
320
+ :start-after: Start Versioned API Example 3
321
+ :end-before: End Versioned API Example 3
322
+
323
+ .. tab::
324
+ :tabid: c
325
+
326
+ .. literalinclude:: /driver-examples/test-mongoc-sample-commands.c
327
+ :language: c
328
+ :dedent: 3
329
+ :start-after: Start Versioned API Example 3
330
+ :end-before: End Versioned API Example 3
331
+
332
+ .. tab::
333
+ :tabid: go
334
+
335
+ .. literalinclude:: /driver-examples/go_examples.go
336
+ :language: go
337
+ :dedent: 0
338
+ :start-after: Start Versioned API Example 3
339
+ :end-before: End Versioned API Example 3
340
+
341
+ .. tab::
342
+ :tabid: csharp
343
+
344
+ .. literalinclude:: /driver-examples/VersionedApiExamples.cs
345
+ :language: csharp
346
+ :dedent: 12
347
+ :start-after: Start Versioned API Example 3
348
+ :end-before: End Versioned API Example 3
349
+
350
+ .. tab::
351
+ :tabid: nodejs
352
+
353
+ .. literalinclude:: /driver-examples/node_versioned_api.js
354
+ :language: nodejs
355
+ :dedent: 4
356
+ :start-after: Start Versioned API Example 3
357
+ :end-before: End Versioned API Example 3
177
358
178
359
Using this non-strict client allows you to run commands outside of the
179
360
Versioned API. For example, this non-strict client now allows you to
0 commit comments