Skip to content

Instantly share code, notes, and snippets.

@joeyAghion
Last active October 10, 2024 21:34
Show Gist options
  • Save joeyAghion/6511184 to your computer and use it in GitHub Desktop.
Save joeyAghion/6511184 to your computer and use it in GitHub Desktop.
List mongodb collections in descending order of size. Helpful for finding largest collections. First number is "size," second is "storageSize."
var collectionNames = db.getCollectionNames(), stats = [];
collectionNames.forEach(function (n) { stats.push(db[n].stats()); });
stats = stats.sort(function(a, b) { return b['size'] - a['size']; });
for (var c in stats) { print(stats[c]['ns'] + ": " + stats[c]['size'] + " (" + stats[c]['storageSize'] + ")"); }
@lfeagan
Copy link

lfeagan commented Oct 10, 2024

I started with the version from terencehonles, but ran into errors when encountering views. So, I added a filter after getting the collection names to inspect the type to verify it is a collection before calling stats().

const readableSize = (size) => {                                               
  const scale = (Math.log(size) / Math.log(1024)) | 0;                         
  return (size / Math.pow(1024, scale)).toFixed(3) + ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'][scale];
}                                                                              
                                                                               
const printStats = (mongo) => {                                                
  mongo.getDBNames()                                                           
    .map((i) => mongo.getDB(i))                                                
    .forEach((db) => {                                                         
      print(db);                                                               
      print(Array(81).join('='));                                              
      db.getCollectionNames()
	.filter((i) => (db.getCollection(i).exists() !== null && db.getCollection(i).exists().type === 'collection'))
        .map((i) => db.getCollection(i).stats())                               
        .sort((a, b) => b.size - a.size)                                       
        .forEach((s) => {                                                      
          print(`${s.ns}: ${readableSize(s.size)} (${readableSize(s.storageSize)})`);
          print(`Indexes: (${readableSize(s.totalIndexSize)})`);               
          Object.entries({...s.indexSizes}).sort((a, b) => b[1] - a[1]).forEach(([n, s]) => {
            print(`  ${n}: ${readableSize(s)}`);                               
          });                                                                  
          print('');                                                           
        });                                                                    
                                                                               
      print('');                                                               
    });                                                                        
}                                                                              

// and used:                                                                               
printStats(db.getMongo())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment