How To
How to list Mongodb Collections in Descending Order of Size
2 min
to list mongodb collections in descending order of size, do the following connect to robo 3t (see this solution article for how to connect to mongodb from outside kubernetes), or use this solution article to access and connect to the mongo shell run the following command function getreadablefilesizestring(filesizeinbytes) { var i = 1; var byteunits = \[' kb', ' mb', ' gb', ' tb', 'pb', 'eb', 'zb', 'yb']; do { filesizeinbytes = filesizeinbytes / 1024; i++; } while (filesizeinbytes > 1024); return math max(filesizeinbytes, 0 1) tofixed(1) + byteunits\[i]; }; var collectionnames = db getcollectionnames(), stats = \[]; collectionnames foreach(function (n) { stats push(db getcollection(n) stats()); }); stats = stats sort(function(a, b) { return b\['size'] a\['size']; }); for (var c in stats) { print(stats\[c]\['ns'] + " " + getreadablefilesizestring(stats\[c]\['size']) + " (" + getreadablefilesizestring(stats\[c]\['storagesize']) + ")"); } you should see a longer version of a list similar to the image below, which is a list of collections in descending order of sizes the first number represents the “size” and the second (in parenthesis) the “storagesize” per mongodb manual on collstats collstats size the total uncompressed size in memory of all records in a collection the size does not include the size of any indexes associated with the collection, which the totalindexsize field reports collstats storagesize the total amount of storage allocated to this collection for document storage the scale argument affects this value since we use wiredtiger and the collection data is compressed the storage size reflects the compressed size and may be smaller than the value for collstats size storagesize does not include index size