|
| 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>`_. |
0 commit comments