5

In MongoDB, WiredTiger provides the Zlib compression option.

To enable the Zlib compression option, I created a collection using the following code.

db.createCollection( "questions", { storageEngine: {
                   wiredTiger: { configString: "blockCompressor=zlib" }}})

I ran some simple tests to measure compression performance and used following data sets storing strings

{
'_id': <ObjectID>,
'question_id': <Five character string>,
'question': <My question>
}

I created another one collection with any compression option using the following code.

db.createCollection( "questions")

Measured the data size using following comment

db.stats(1024*1024).dataSize + db.stats(1024*1024).indexSize

But I can't able to see the compression difference between these two collections. I referred the following links to achieve my process.

https://www.mongodb.com/blog/post/new-compression-options-mongodb-30

https://scalegrid.io/blog/enabling-data-compression-in-mongodb-3-0/

2 Answers 2

7

Don't use dataSize for this comparison since it's the uncompressed size. Use storageSize instead.

Using MongoDB 4.2.2 for example:

// create collections
> db.createCollection('snappy')
> db.createCollection('zlib', {storageEngine: {wiredTiger: {configString: 'block_compressor=zlib'}}})

// insert a compressible document into both collections
> doc = {_id:0, text:<a paragraph of text>}
> db.snappy.insert(doc)
> db.zlib.insert(doc)

// storage size comparison
> db.snappy.stats().storageSize
20480
> db.zlib.stats().storageSize
4096

// data size comparison
> db.snappy.dataSize()
697
> db.zlib.dataSize()
697

So the storage size of zlib is much smaller than default (snappy), but data size are the same between them.

Note: If you're still using it, MongoDB 3.0 is very much outdated and not supported anymore since February 2018. Please use a more recent version (latest as of Jan 2020 is 4.2.2).

1
0

db.createCollection( "questions", { storageEngine: { wiredTiger: { configString: "blockCompressor=zlib" }}})

ERROR -- throws invalid argument

use instead

db.createCollection( "email", { storageEngine: {wiredTiger: { configString: "block_compressor=zlib" }}})

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jan 9 at 13:24

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.