Skip to content

Instantly share code, notes, and snippets.

@dimi-kr
Forked from BlakeGardner/compact.js
Created May 18, 2018 09:02
Show Gist options
  • Save dimi-kr/e5143bff7f90888b16d7d2c0038b1f30 to your computer and use it in GitHub Desktop.
Save dimi-kr/e5143bff7f90888b16d7d2c0038b1f30 to your computer and use it in GitHub Desktop.
Compact all collections inside of a MongoDB database
// This script loops though the list of collection names in a MongoDB and runs the compact operation on them
// Simply paste this into the Mongo shell
use testDbName;
db.getCollectionNames().forEach(function (collectionName) {
print('Compacting: ' + collectionName);
db.runCommand({ compact: collectionName });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment