|
| 1 | +.. _pymongo-compression: |
| 2 | + |
| 3 | +=========== |
| 4 | +Compression |
| 5 | +=========== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. contents:: On this page |
| 12 | + :local: |
| 13 | + :backlinks: none |
| 14 | + :depth: 2 |
| 15 | + :class: singlecol |
| 16 | + |
| 17 | +Overview |
| 18 | +-------- |
| 19 | + |
| 20 | +In this guide, you can learn how to use compression algorithms with |
| 21 | +{+driver-short+}. |
| 22 | + |
| 23 | +You can use compression to reduce the size of messages sent between your application |
| 24 | +and a MongoDB deployment. {+driver-short+} supports the following compression algorithms: |
| 25 | + |
| 26 | +- `Snappy <https://google.github.io/snappy/>`__: You can use Snappy compression |
| 27 | + with MongoDB 3.4 and later by including the `python-snappy <https://pypi.org/project/python-snappy/>`__ |
| 28 | + package in your application. |
| 29 | +- `Zlib <https://zlib.net/>`__: You can use Zlib compression with MongoDB 3.6 and later |
| 30 | + by including the `zlib <https://docs.python.org/3/library/zlib.html>`__ package |
| 31 | + in your application. |
| 32 | +- `Zstandard <https://github.com/facebook/zstd/>`__: You can use Zstandard compression |
| 33 | + with MongoDB 4.2 and later by including the `zstandard <https://pypi.org/project/zstandard/>`__ |
| 34 | + package in your application. |
| 35 | + |
| 36 | +Specifying Compression By Using a Connection String |
| 37 | +--------------------------------------------------- |
| 38 | + |
| 39 | +You can specify the compression algorithm to use by including the ``compressors`` option |
| 40 | +in your connection string. The following example specifies the Snappy compression algorithm |
| 41 | + |
| 42 | +.. code-block:: python |
| 43 | + |
| 44 | + client = pymongo.MongoClient("mongodb://localhost/?compressors=snappy") |
| 45 | + |
| 46 | +You can also specify multiple compression algorithms by separating them with a comma, as |
| 47 | +show in the following example: |
| 48 | + |
| 49 | +.. code-block:: python |
| 50 | + |
| 51 | + client = pymongo.MongoClient("mongodb://localhost/?compressors=snappy,zlib,zstd") |
| 52 | + |
| 53 | +.. note:: |
| 54 | + |
| 55 | + When you supply multiple compression algorithms to a connection string, |
| 56 | + {+driver-short+} uses the first compression algorithm in the list that the |
| 57 | + deployment supports. |
| 58 | + |
| 59 | +Specifying Compression to a MongoClient |
| 60 | +--------------------------------------- |
| 61 | + |
| 62 | +You can also specify the compression algorithm to use by passing the algorithms to use |
| 63 | +to the ``compressors`` parameter of the ``MongoClient`` constructor, as shown in the |
| 64 | +following example: |
| 65 | + |
| 66 | +.. code-block:: python |
| 67 | + |
| 68 | + client = pymongo.MongoClient(compressors="snappy") |
| 69 | + |
| 70 | +You can also specify multiple compression algorithms by passing a list of algorithms to the |
| 71 | +``MongoClient`` constructor, as shown in the following example: |
| 72 | + |
| 73 | +.. code-block:: python |
| 74 | + |
| 75 | + client = pymongo.MongoClient(compressors=["snappy", "zlib", "zstd"]) |
| 76 | + |
| 77 | +.. note:: |
| 78 | + |
| 79 | + When you supply multiple compression algorithms to a ``MongoClient``, |
| 80 | + {+driver-short+} uses the first compression algorithm in the list that the |
| 81 | + deployment supports. |
| 82 | + |
| 83 | +API Documentation |
| 84 | +----------------- |
| 85 | + |
| 86 | +To learn more about any of the methods or types discussed in this |
| 87 | +guide, see the following API documentation: |
| 88 | + |
| 89 | +- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__ |
0 commit comments