Skip to content

Instantly share code, notes, and snippets.

View daniel-sc's full-sized avatar

Daniel Schreiber daniel-sc

  • ti&m
  • Offenbach, Germany
View GitHub Profile
@daniel-sc
daniel-sc / compact.js
Last active May 17, 2023 08:03 — forked from BlakeGardner/compact.js
Compact all collections inside of a MongoDB database
// This script loops though all collections of all db in a MongoDB and runs the compact operation on them
// Simply paste this into the Mongo shell
rs.secondaryOk();
db.getMongo().getDBNames().forEach(function(dbName) {
if ("local" != dbName && "admin" != dbName && "system" != dbName /* use this to (re-)start: && dbName > "am"*/) {
var subject = db.getSiblingDB(dbName);
subject.getCollectionNames().forEach(function (collectionName) {
print(new Date() + ': Compacting: ' + dbName + " - " + collectionName);
sleep(1000); // assure a cancel (CTRL-C) after "done" is executed before compact command