Skip to content

Commit 01e5bc5

Browse files
jmd-mongojeff-allen-mongo
authored andcommitted
DOCSP-17366 These updates add driver examples to the Versioned API page
1 parent 22ce702 commit 01e5bc5

File tree

1 file changed

+208
-27
lines changed

1 file changed

+208
-27
lines changed

source/reference/versioned-api.txt

Lines changed: 208 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Versioned API
1212

1313
What is the Versioned API, and Should You Use It?
1414
-------------------------------------------------
15-
15+
1616
The MongoDB Versioned API lets you upgrade your MongoDB server at will,
1717
and ensure that behavior changes between MongoDB versions do not break
1818
your application.
@@ -44,36 +44,168 @@ resulting from server upgrades. This guarantee holds as long as the
4444
new server supports your specified API version.
4545

4646
To guarantee backward compatibility, your application must:
47-
47+
4848
- Declare an API version
4949
- Only use commands and features supported in your specified API version
5050
- Deploy with a supported version of an official driver
5151

5252
Declare the API Version
5353
-----------------------
5454

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+
5564
To use the Versioned API, upgrade to the latest driver and create your
5665
application's MongoClient:
5766

58-
.. code-block:: none
67+
.. tabs-selector:: drivers
68+
69+
.. tabs-drivers::
70+
71+
.. tab::
72+
:tabid: shell
5973

60-
client = MongoClient(
61-
<connection string>,
62-
api={"version": "1", "strict": True}
63-
)
74+
.. code-block:: javascript
75+
76+
mongosh --apiVersion 1
6477

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
6880

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
7086

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
72131

73132
``"1"`` is currently the only API version available.
74133

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
77209

78210
Migrate To Versioned API Commands
79211
---------------------------------
@@ -135,7 +267,7 @@ However, the :dbcommand:`aggregate` command is
135267
obtain a count in the following way:
136268

137269
.. code-block:: javascript
138-
270+
139271
db.sales.aggregate([
140272
{
141273
$group: {
@@ -157,23 +289,72 @@ How To Use Commands and Features Outside of the Versioned API
157289

158290
To use commands and features outside of the Versioned API, you can
159291
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
161300

162-
.. code-block:: none
301+
.. code-block:: javascript
302+
303+
mongosh --apiVersion 1
163304

164-
# Non-strict client
165-
client = MongoClient(
166-
<connection string>,
167-
api={"version": "1", "strict": False}
168-
)
305+
.. tab::
306+
:tabid: python
169307

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
173313

174-
.. code-block:: shell
314+
.. tab::
315+
:tabid: php
175316

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
177358

178359
Using this non-strict client allows you to run commands outside of the
179360
Versioned API. For example, this non-strict client now allows you to

0 commit comments

Comments
 (0)