|
19 | 19 | MongoDB Versioned API
|
20 | 20 | =====================
|
21 | 21 |
|
| 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 | +
|
22 | 32 | To configure MongoDB Versioned API, pass the ``server_api`` keyword option to
|
23 | 33 | :class:`~pymongo.mongo_client.MongoClient`::
|
24 | 34 |
|
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) |
27 | 41 |
|
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. |
29 | 45 |
|
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. |
31 | 54 |
|
32 | 55 | Strict Mode
|
33 | 56 | ```````````
|
34 | 57 |
|
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:: |
37 | 63 |
|
38 |
| - >>> client = MongoClient(server_api=ServerApi('1', strict=True)) |
| 64 | + >>> server_api = ServerApi('1', strict=True) |
| 65 | + >>> client = MongoClient(server_api=server_api) |
39 | 66 | >>> client.test.command('count', 'test')
|
40 | 67 | Traceback (most recent call last):
|
41 | 68 | ...
|
42 | 69 | 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'
|
43 | 70 |
|
| 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 | +
|
44 | 83 | Classes
|
45 | 84 | =======
|
46 | 85 | """
|
|
0 commit comments