Docs Menu
Docs Home
/ / /
Ruby Driver
/ /

Compress Network Traffic

The Ruby driver supports network compression to reduce the amount of data transmitted between the client and the server.

The driver supports the following compression algorithms:

  • Snappy

  • Zlib

  • Zstandard

If you specify multiple compression algorithms, the driver selects the first one in the list supported by your MongoDB instance.

To enable compression for the connection to your MongoDB instance, specify the algorithms you want to use in one of the following ways:

  • Add the algorithms to your connection string as a parameter

  • Specify the algorithms in the compressors option of your Mongo::Client object

To enable network compression by using the connection string, add the compressors option. You can specify one or more algorithms as a comma-separated list.

uri = "mongodb://<hostname>:<port>/?compressors=zlib,snappy"
client = Mongo::Client.new(uri)

To enable compression in your Client object, pass the compressors option to the Mongo::Client constructor.

client = Mongo::Client.new(["<hostname>:<port>"],
compressors: ["zlib", "snappy"])

Back

Connection Options

On this page