Skip to content

PYTHON-2536 Document Versioned API Usage #584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions pymongo/server_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,67 @@
MongoDB Versioned API
=====================

Starting in MongoDB 5.0, applications can specify the server API version
to use when creating a :class:`~pymongo.mongo_client.MongoClient`. Doing so
ensures that the driver behaves in a manner compatible with that server API
version, regardless of the server's actual release version.

Declaring an API Version
````````````````````````

.. attention:: Versioned API requires MongoDB >=5.0.

To configure MongoDB Versioned API, pass the ``server_api`` keyword option to
:class:`~pymongo.mongo_client.MongoClient`::

from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
>>> from pymongo.mongo_client import MongoClient
>>> from pymongo.server_api import ServerApi
>>>
>>> # Declare API version "1" for MongoClient "client"
>>> server_api = ServerApi('1')
>>> client = MongoClient(server_api=server_api)

client = MongoClient(server_api=ServerApi('1'))
The declared API version is applied to all commands run through ``client``,
including those sent through the generic
:meth:`~pymongo.database.Database.command` helper.

Note that Versioned API requires MongoDB >=5.0.
.. note:: Declaring an API version on the
:class:`~pymongo.mongo_client.MongoClient` **and** specifying versioned
API options in :meth:`~pymongo.database.Database.command` command document
is not supported and will lead to undefined behaviour.

To run any command without declaring a server API version or using a different
API version, create a separate :class:`~pymongo.mongo_client.MongoClient`
instance.

Strict Mode
```````````

When ``strict`` mode is configured, commands that are not supported in the
given :attr:`ServerApi.version` will fail. For example::
Configuring ``strict`` mode will cause the MongoDB server to reject all
commands that are not part of the declared :attr:`ServerApi.version`. This
includes command options and aggregation pipeline stages.

For example::

>>> client = MongoClient(server_api=ServerApi('1', strict=True))
>>> server_api = ServerApi('1', strict=True)
>>> client = MongoClient(server_api=server_api)
>>> client.test.command('count', 'test')
Traceback (most recent call last):
...
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'

Detecting API Deprecations
``````````````````````````

The ``deprecationErrors`` option can be used to enable command failures
when using functionality that is deprecated from the configured
:attr:`ServerApi.version`. For example::

>>> server_api = ServerApi('1', deprecation_errors=True)
>>> client = MongoClient(server_api=server_api)

Note that at the time of this writing, no deprecated APIs exist.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have an example of what a deprecation would look like? Would it be an OperationFailure error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>>> client.db.command(command={'testDeprecationInVersion2': 1, 'apiVersion': '2', 'apiDeprecationErrors': True})
...
pymongo.errors.OperationFailure: Provided apiDeprecationErrors:true, but the command testDeprecationInVersion2 is deprecated in API Version 2, full error: {'ok': 0.0, 'errmsg': 'Provided apiDeprecationErrors:true, but the command testDeprecationInVersion2 is deprecated in API Version 2', 'code': 324, 'codeName': 'APIDeprecationError', '$clusterTime': {'clusterTime': Timestamp(1617170403, 1), 'signature': {'hash': b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 'keyId': 0}}, 'operationTime': Timestamp(1617170403, 1)}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm reluctant to include this as an example because the server needs to be started with special testing parameters enabled to be able to see this behavior.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM.


Classes
=======
"""
Expand Down