1// Switch to admin database and get list of databases.
2db = db.getSiblingDB("admin");
3dbs = db.runCommand({ "listDatabases": 1 }).databases;
4// Iterate through each database and get its collections.
5dbs.forEach(function(database) {
6 db = db.getSiblingDB(database.name);
7 cols = db.getCollectionNames();
8 // Iterate through each collection.
9 cols.forEach(function(col) {
10 // Do something with each collection.
11 print(col);
12 });
13});