Network Compression
On this page
Overview
In this guide, you can learn how to enable network compression by using the Go driver. You can specify a client option to compress messages, which reduces the amount of data passed over the network between MongoDB and your application.
The Go driver supports the following compression algorithms:
Snappy: available in MongoDB 3.4 and later.
Zlib: available in MongoDB 3.6 and later.
Zstandard: available in MongoDB 4.2 and later.
If you specify multiple compression algorithms, the driver selects the first one in the list supported by your MongoDB deployment.
You must add dependencies to use the Snappy or Zstandard compression algorithm. For more information, see the Compression Algorithm Dependencies section of this guide.
Specify Compression Algorithms
You can enable compression for the connection to your MongoDB deployment by specifying the algorithms in one of two ways:
Set the compression algorithm in your connection string.
Set the compression algorithm in a
ClientOptions
instance.
Specify compression algorithms by using the following strings:
Compression Algorithm Dependencies
To add the Snappy compression algorithm to your application, run the following code:
go get github.com/golang/snappy
To add the Zstandard compression algorithm to your application, run the following code:
go get -u github.com/klauspost/compress
To add the Zlib compression algorithm to your application, import the built-in
zlib
package. You must add the following import statement to application files
that instantiate a Client
with Zlib compression:
import "compress/zlib"
Additional Information
For more information about the concepts in this guide, see the following documentation:
Connection URI in the Connection Guide
Connection String Compression Options in the Server manual
The zlib package Go documentation