How to get all collections from all databases in MongoDB

In this article, we will see how to get all collections from all databases in in MongoDB Server. We can get all databases and the collections in them by using db.getMongo() and db.getDBNames() methods with forEach method.

1. Run the following command from mongo shell.

db.getMongo().getDBNames().forEach( function(v, i){ print( v + '\n\t' + db.getSiblingDB(v).getCollectionNames().join('\n\t'))} )

2. Run the following query from the Linux shell.

mongo --eval " db.getMongo().getDBNames().forEach( function(v, i){ print( v + '\n\t' + db.getSiblingDB(v).getCollectionNames().join('\n\t'))} )"

3. Both will give same output:

admin system.keys system.users system.version config system.sessions transactions example test examples test flights flightData local oplog.rs replset.election replset.minvalid replset.oplogTruncateAfterPoint startup_log system.replset system.rollback.id m201 people products students supplies money employees myDb analytics books hello stores tab technology test1 testData users r2schools addressBook employees inventory records student survey test test

Leave a Reply

Your email address will not be published. Required fields are marked *