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 each algorithm and the dependency it requires, if any:
Compression Algorithm | Dependency |
|---|---|
Snappy | Add the Snappy compression algorithm to your application by running the |
Zlib | The driver uses the built-in |
Zstandard | Add the Zstandard compression algorithm to your application by running the |
Specify Compression Algorithms
You can enable compression for the connection to your MongoDB deployment by specifying the algorithms in one of the following ways:
Set the compression algorithm in your connection string
Set the compression algorithm in a
ClientOptionsinstance
To enable compression by using the connection string, add the compression algorithm as the value of the compressors parameter to your connection string. You can specify one or more compression algorithms separated by commas:
opts := options.Client().ApplyURI("mongodb://localhost:27017/?compressors=snappy,zlib,zstd")client, _ := mongo.Connect(opts)To enable compression by specifying a ClientOptions instance, pass one or more compression algorithms to the SetCompressors() method as a string array:
opts := options.Client().SetCompressors([]string{"snappy", "zlib", "zstd"})client, _ := mongo.Connect(opts)Specify compression algorithms by using the following strings:
Additional Information
For more information about the concepts in this guide, see the following documentation:
Connection URI section of the Create a MongoClient guide.
Connection String Compression Options in the Server manual
The zlib package Go documentation