Last active
February 19, 2024 16:55
-
-
Save BlakeGardner/8548102 to your computer and use it in GitHub Desktop.
Compact all collections inside of a MongoDB database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
db.getCollectionNames
returns views too 🙃https://www.mongodb.com/docs/manual/reference/method/db.getCollectionNames/
If you want collection names only you need something like
db.getCollectionInfos({ type: 'collection' }).map(v => v.name).filter(v => v != 'system.views')