Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
MongoDB/WiredTiger - compress all collections in a database.
colls = db.getCollectionNames();
for (var i = 0; i < colls.length; i++) {
var coll = colls[i];
if (coll.match(/^system/)) continue;
if (coll.match(/_old$/) || coll.match(/_comp$/)) {
stats = db[coll].stats();
if (stats.wiredTiger.creationString.match(/snappy/)) continue;
db.createCollection(coll + "_comp", {storageEngine: {wiredTiger: {configString: 'block_compressor=snappy'}}});
db[coll].find().forEach(function(o) {
db[coll + "_comp"].save(o);
db[coll].renameCollection(coll + "_old");
db[coll + "_comp"].renameCollection(coll);

This comment has been minimized.

Copy link
Owner Author

@mattparlane mattparlane commented Feb 26, 2015

Make sure you don't already have collections where name =~ /_old$/ || name =~ /_comp$/, or they will be deleted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment