Skip to content

Commit a09a03e

Browse files
authored
PYTHON-3930 Add docs page for network compression (#1415)
1 parent 4b9c5b9 commit a09a03e

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

doc/changelog.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ PyMongo 4.6 brings a number of improvements including:
3030
"mongodb://example.com?tls=true" is now a valid URI.
3131
- Fixed a bug where PyMongo would incorrectly promote all cursors to exhaust cursors
3232
when connected to load balanced MongoDB clusters or Serverless clusters.
33+
- Added the :ref:`network-compression-example` documentation page.
3334

3435
Changes in Version 4.5
3536
----------------------
@@ -1278,8 +1279,8 @@ Version 3.7 adds support for MongoDB 4.0. Highlights include:
12781279

12791280
- Support for single replica set multi-document ACID transactions.
12801281
See :ref:`transactions-ref`.
1281-
- Support for wire protocol compression. See the
1282-
:meth:`~pymongo.mongo_client.MongoClient` documentation for details.
1282+
- Support for wire protocol compression via the new ``compressors`` URI and keyword argument to
1283+
:meth:`~pymongo.mongo_client.MongoClient`. See :ref:`network-compression-example` for details.
12831284
- Support for Python 3.7.
12841285
- New count methods, :meth:`~pymongo.collection.Collection.count_documents`
12851286
and :meth:`~pymongo.collection.Collection.estimated_document_count`.

doc/examples/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ MongoDB, you can start it like so:
2828
gridfs
2929
high_availability
3030
mod_wsgi
31+
network_compression
3132
server_selection
3233
tailable
3334
timeouts

doc/examples/network_compression.rst

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
.. _network-compression-example:
3+
4+
Network Compression
5+
===================
6+
7+
PyMongo supports network compression where network traffic between the client
8+
and MongoDB server are compressed which reduces the amount of data passed
9+
over the network. By default no compression is used.
10+
11+
The driver supports the following algorithms:
12+
13+
- `snappy <https://pypi.org/project/python-snappy>`_ available in MongoDB 3.4 and later.
14+
- :mod:`zlib` available in MongoDB 3.6 and later.
15+
- `zstandard <https://pypi.org/project/zstandard/>`_ available in MongoDB 4.2 and later.
16+
17+
.. note:: snappy and zstandard compression require additional dependencies. See :ref:`optional-deps`.
18+
19+
Applications can enable wire protocol compression via the ``compressors`` URI and
20+
keyword argument to :meth:`~pymongo.mongo_client.MongoClient`. For example::
21+
22+
>>> client = MongoClient(compressors='zlib')
23+
24+
When multiple compression algorithms are given, the driver selects the first one in the
25+
list supported by the MongoDB instance to which it is connected. For example::
26+
27+
>>> client = MongoClient(compressors='snappy,zstandard,zlib')
28+
29+
The ``compressors`` option can also be set via the URI::
30+
31+
>>> client = MongoClient('mongodb://example.com/?compressors=snappy,zstandard,zlib')
32+
33+
Additionally, zlib compression allows specifying a compression level with supported values from -1 to 9::
34+
35+
>>> client = MongoClient(compressors='zlib', zlibCompressionLevel=-1)
36+
37+
The ``zlibCompressionLevel`` is passed as the ``level`` argument to :func:`zlib.compress`.
38+
39+
.. seealso:: The MongoDB documentation on `network compression URI options <https://dochub.mongodb.org/core/compression-options>`_.

doc/installation.rst

+5-2
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ Dependencies
3030

3131
PyMongo supports CPython 3.7+ and PyPy3.7+.
3232

33-
Required dependencies:
33+
Required dependencies
34+
.....................
3435

3536
Support for mongodb+srv:// URIs requires `dnspython
3637
<https://pypi.python.org/pypi/dnspython>`_
3738

39+
.. _optional-deps:
3840

39-
Optional dependencies:
41+
Optional dependencies
42+
.....................
4043

4144
GSSAPI authentication requires `pykerberos
4245
<https://pypi.python.org/pypi/pykerberos>`_ on Unix or `WinKerberos

pymongo/mongo_client.py

+1
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ def __init__(
402402
package. By default no compression is used. Compression support
403403
must also be enabled on the server. MongoDB 3.6+ supports snappy
404404
and zlib compression. MongoDB 4.2+ adds support for zstd.
405+
See :ref:`network-compression-example` for details.
405406
- `zlibCompressionLevel`: (int) The zlib compression level to use
406407
when zlib is used as the wire protocol compressor. Supported values
407408
are -1 through 9. -1 tells the zlib library to use its default

0 commit comments

Comments
 (0)