Docs Menu
Docs Home
/
PyMongo
/

Compression

On this page

  • Overview
  • Specifying Compression By Using a Connection String
  • Specifying Compression to a MongoClient
  • API Documentation

In this guide, you can learn how to use compression algorithms with PyMongo.

You can use compression to reduce the size of messages sent between your application and a MongoDB deployment. PyMongo supports the following compression algorithms:

  • Snappy: You can use Snappy compression with MongoDB 3.4 and later by including the python-snappy package in your application.

  • Zlib: You can use Zlib compression with MongoDB 3.6 and later
    by including the zlib package in your application.
  • Zstandard: You can use Zstandard compression with MongoDB 4.2 and later by including the zstandard package in your application.

You can specify the compression algorithm to use by including the compressors option in your connection string. The following example specifies the Snappy compression algorithm

client = pymongo.MongoClient("mongodb://localhost/?compressors=snappy")

You can also specify multiple compression algorithms by separating them with a comma, as show in the following example:

client = pymongo.MongoClient("mongodb://localhost/?compressors=snappy,zlib,zstd")

Note

When you supply multiple compression algorithms to a connection string, PyMongo uses the first compression algorithm in the list that the deployment supports.

You can also specify the compression algorithm to use by passing the algorithms to use to the compressors parameter of the MongoClient constructor, as shown in the following example:

client = pymongo.MongoClient(compressors="snappy")

You can also specify multiple compression algorithms by passing a list of algorithms to the MongoClient constructor, as shown in the following example:

client = pymongo.MongoClient(compressors=["snappy", "zlib", "zstd"])

Note

When you supply multiple compression algorithms to a MongoClient, PyMongo uses the first compression algorithm in the list that the deployment supports.

To learn more about any of the methods or types discussed in this guide, see the following API documentation:

Back

Connection Pools