Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display all databases along with their sizes in ASCENDING/DESCENDING order in MongoDB

Tags:

mongodb

I have more than 600 databases in my MongoDB system. Whenever i run command show dbs it returns databases with their sizes in alphabetical order. I want to get databases in Ascending or Descending order.
Is that possible in MongoDB ?

like image 549
Abhinandan Kothari Avatar asked Nov 30 '22 16:11

Abhinandan Kothari


1 Answers

You can write simple java scripts inside shell

db.adminCommand("listDatabases").databases
    .sort(function(l, r) {
         return r.sizeOnDisk - l.sizeOnDisk})
    .forEach(function(d) {
         print(d.name + " - " + d.sizeOnDisk)});

Or invoke shell with passing the script:

mongo --quiet  --eval 'db.adminCommand("listDatabases").databases.sort(function(l, r) {return r.sizeOnDisk - l.sizeOnDisk}).forEach(function(d) {print(d.name + " - " + d.sizeOnDisk)});'
like image 92
Mike Shauneu Avatar answered Dec 06 '22 10:12

Mike Shauneu