Skip to content

Instantly share code, notes, and snippets.

@paulozullu
Forked from BlakeGardner/compact.js
Created August 13, 2018 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulozullu/2fa3b3ee96aab994db39effce3874e1b to your computer and use it in GitHub Desktop.
Save paulozullu/2fa3b3ee96aab994db39effce3874e1b 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