Skip to content

Instantly share code, notes, and snippets.

@mattparlane
Last active August 29, 2015 14:16
Show Gist options
  • Save mattparlane/a8c39260f034eedec820 to your computer and use it in GitHub Desktop.
Save mattparlane/a8c39260f034eedec820 to your computer and use it in GitHub Desktop.
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$/)) {
db[coll].drop();
continue;
}
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);
}
@mattparlane
Copy link
Author

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