Configuring the mongodb node.js driver to use snappy compression
You have two options to enable compression:
1) Driver option
Set the driver option when you initialize the MongoClient. Depending on the driver the syntax may differ. See docs, for example:
"options": { "compression": [ "zstd", "snappy", "zlib" ]}
2) Connection string
Add the compressors
argument to your connection string, for example:
mongodb://example.com/?compressors=zstd,snappy,zlib
Note
The examples above demonstrate how to set multiple compressors to better explain the syntax and make the answer more useful. If you only want to set only one compressor, change accordingly.
You can find more examples on how to set compressors in the mongoDB Node.js driver test scripts.
You can verify that network compression is begin used and which compression algorithm is being used in the mongo logs, as each compression / decompression generates a verbose log entry. Or you can run
db.serverStatus()['network']
and observe the bytesIn / bytesOut of various compressors, e.g.:{ "bytesIn" : NumberLong("1828061251"), "bytesOut" : NumberLong("57900955809"), "physicalBytesIn" : NumberLong("2720120753"), "physicalBytesOut" : NumberLong("32071382239"), "numRequests" : NumberLong("570858"), "compression" : { "snappy" : { "compressor" : { "bytesIn" : NumberLong("2215000774"), "bytesOut" : NumberLong("752759260") }, "decompressor" : { "bytesIn" : NumberLong("226402961"), "bytesOut" : NumberLong("848171447") } } }, "serviceExecutorTaskStats" : { "executor" : "passthrough", "threadsRunning" : 80 }}