Version Compatibility
The following table lists the minimum supported driver and MongoDB Server version for each compression algorithm:
Enable Network Compression
To enable network compression for your MongoDB connection, install the required dependencies and specify the compression algorithms that you want to use.
Install Dependencies
The following table lists the dependencies required for each compression algorithm:
Compression Algorithm | Dependency |
|---|---|
Snappy | Requires the python-snappy package. |
Zlib | Requires the Zlib module, included in the standard Python library. |
Zstandard | Requires the zstandard package. |
Specify Compression Algorithms
To enable compression for the connection to your MongoDB instance, use the compressors connection option and specify the compression algorithms you want to use. You can do this in two ways:
Pass the algorithms as an argument to the
MongoClientconstructor.Specify the algorithms in your connection string.
The following code example shows both options:
The following tabs demonstrate how to specify all available compressors while connecting to MongoDB:
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>", compressors = "snappy,zstd,zlib")
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?" "compressors=snappy,zstd,zlib") client = pymongo.MongoClient(uri)
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>", compressors = "snappy,zstd,zlib")
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?" "compressors=snappy,zstd,zlib") client = pymongo.AsyncMongoClient(uri)
If you don't specify a compression algorithm, PyMongo doesn't compress your network traffic. If you specify multiple compression algorithms, the driver selects the first one in the list supported by your MongoDB instance.
Specify the Zlib Compression Level
If you specify zlib as one of your compression algorithms, you can also use the zlibCompressionLevel option to specify a compression level. This option accepts an integer value between -1 and 9:
-1: (Default). zlib uses its default compression level (usually
6).0: No compression.
1: Fastest speed but lowest compression.
9: Best compression but slowest speed.
The following code example specifies the zlib compression algorithm and a value of 1 for the zlibCompressionLevel option:
client = pymongo.MongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>", compressors = "zlib", zlibCompressionLevel=1)
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?" "compressors=zlib" "zlibCompressionLevel=1") client = pymongo.MongoClient(uri)
client = pymongo.AsyncMongoClient("mongodb://<db_username>:<db_password>@<hostname>:<port>", compressors = "zlib", zlibCompressionLevel=1)
uri = ("mongodb://<db_username>:<db_password>@<hostname>:<port>/?" "compressors=zlib" "zlibCompressionLevel=1") client = pymongo.AsyncMongoClient(uri)
API Documentation
To learn more about any of the methods or types discussed in this guide, see the MongoClient API documentation.