Skip to content

Commit 1882e99

Browse files
PYTHON-2536 Document versioned API usage (#584)
1 parent 97bad5a commit 1882e99

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

pymongo/server_api.py

+46-7
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,67 @@
1919
MongoDB Versioned API
2020
=====================
2121
22+
Starting in MongoDB 5.0, applications can specify the server API version
23+
to use when creating a :class:`~pymongo.mongo_client.MongoClient`. Doing so
24+
ensures that the driver behaves in a manner compatible with that server API
25+
version, regardless of the server's actual release version.
26+
27+
Declaring an API Version
28+
````````````````````````
29+
30+
.. attention:: Versioned API requires MongoDB >=5.0.
31+
2232
To configure MongoDB Versioned API, pass the ``server_api`` keyword option to
2333
:class:`~pymongo.mongo_client.MongoClient`::
2434
25-
from pymongo.mongo_client import MongoClient
26-
from pymongo.server_api import ServerApi
35+
>>> from pymongo.mongo_client import MongoClient
36+
>>> from pymongo.server_api import ServerApi
37+
>>>
38+
>>> # Declare API version "1" for MongoClient "client"
39+
>>> server_api = ServerApi('1')
40+
>>> client = MongoClient(server_api=server_api)
2741
28-
client = MongoClient(server_api=ServerApi('1'))
42+
The declared API version is applied to all commands run through ``client``,
43+
including those sent through the generic
44+
:meth:`~pymongo.database.Database.command` helper.
2945
30-
Note that Versioned API requires MongoDB >=5.0.
46+
.. note:: Declaring an API version on the
47+
:class:`~pymongo.mongo_client.MongoClient` **and** specifying versioned
48+
API options in :meth:`~pymongo.database.Database.command` command document
49+
is not supported and will lead to undefined behaviour.
50+
51+
To run any command without declaring a server API version or using a different
52+
API version, create a separate :class:`~pymongo.mongo_client.MongoClient`
53+
instance.
3154
3255
Strict Mode
3356
```````````
3457
35-
When ``strict`` mode is configured, commands that are not supported in the
36-
given :attr:`ServerApi.version` will fail. For example::
58+
Configuring ``strict`` mode will cause the MongoDB server to reject all
59+
commands that are not part of the declared :attr:`ServerApi.version`. This
60+
includes command options and aggregation pipeline stages.
61+
62+
For example::
3763
38-
>>> client = MongoClient(server_api=ServerApi('1', strict=True))
64+
>>> server_api = ServerApi('1', strict=True)
65+
>>> client = MongoClient(server_api=server_api)
3966
>>> client.test.command('count', 'test')
4067
Traceback (most recent call last):
4168
...
4269
pymongo.errors.OperationFailure: Provided apiStrict:true, but the command count is not in API Version 1, full error: {'ok': 0.0, 'errmsg': 'Provided apiStrict:true, but the command count is not in API Version 1', 'code': 323, 'codeName': 'APIStrictError'
4370
71+
Detecting API Deprecations
72+
``````````````````````````
73+
74+
The ``deprecationErrors`` option can be used to enable command failures
75+
when using functionality that is deprecated from the configured
76+
:attr:`ServerApi.version`. For example::
77+
78+
>>> server_api = ServerApi('1', deprecation_errors=True)
79+
>>> client = MongoClient(server_api=server_api)
80+
81+
Note that at the time of this writing, no deprecated APIs exist.
82+
4483
Classes
4584
=======
4685
"""

0 commit comments

Comments
 (0)