Skip to content

Instantly share code, notes, and snippets.

View daniel-sc's full-sized avatar

Daniel Schreiber daniel-sc

  • ti&m
  • Offenbach, Germany
  • 16:36 (UTC +02:00)
View GitHub Profile
@daniel-sc
daniel-sc / compact.js
Last active February 2, 2025 19:06 — 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
db.getMongo().setReadPref('secondary');
db.getMongo().getDBNames().forEach(function(dbName) {
if (["local", "admin", "system", "config"].indexOf(dbName) < 0 /* && dbName > "config"*/) {
var subject = db.getSiblingDB(dbName);
subject.getCollectionNames()
.filter(c => ['system.users', 'system.roles'].indexOf(c) < 0)
.forEach(function (collectionName) {