|
| 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 supported 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 | +- `Zstandard <https://github.com/facebook/zstd/>`__: You can use Zstandard compression |
| 31 | + with MongoDB 4.2 and later by including the `zstandard <https://pypi.org/project/zstandard/>`__ |
| 32 | + package in your application. |
| 33 | + |
| 34 | +Specifying Compression By Using a Connection String |
| 35 | +--------------------------------------------------- |
| 36 | + |
| 37 | +You can specify the compression algorithm to use by including the ``compressors`` option |
| 38 | +in yoru connection string. The following example specifies the Snappy compression algorithm |
| 39 | + |
| 40 | +.. code-block:: python |
| 41 | + |
| 42 | + client = pymongo.MongoClient("mongodb://localhost/?compressors=snappy") |
| 43 | + |
| 44 | +You can also specify multiple compression algorithms by separating them with a comma, as |
| 45 | +show in the following example: |
| 46 | + |
| 47 | +.. code-block:: python |
| 48 | + |
| 49 | + client = pymongo.MongoClient("mongodb://localhost/?compressors=snappy,zlib,zstd") |
| 50 | + |
| 51 | +Specifying Compression to a MongoClient |
| 52 | +--------------------------------------- |
| 53 | + |
| 54 | +You can also specify the compression algorithm to use when creating a ``MongoClient`` object, |
| 55 | +as shown in the following example: |
| 56 | + |
| 57 | +.. code-block:: python |
| 58 | + |
| 59 | + client = pymongo.MongoClient(compressors="snappy") |
| 60 | + |
| 61 | +You can also specify multiple compression algorithms by passing a list of algorithms to the |
| 62 | +``MongoClient`` constructors, as shown in the following example: |
| 63 | + |
| 64 | +.. code-block:: python |
| 65 | + |
| 66 | + client = pymongo.MongoClient(compressors=["snappy", "zlib", "zstd"]) |
| 67 | + |
| 68 | +.. note:: |
| 69 | + |
| 70 | + When you supply multiple compression algorithms to a ``MongoClient``, |
| 71 | + {+driver-short+} uses the first compression algorithm in the list that the |
| 72 | + deployment supports. |
| 73 | + |
| 74 | +API Documentation |
| 75 | +----------------- |
| 76 | + |
| 77 | +To learn more about any of the methods or types discussed in this |
| 78 | +guide, see the following API documentation: |
| 79 | + |
| 80 | +- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__ |
0 commit comments