Skip to content

Instantly share code, notes, and snippets.

@jonmaim
Last active February 13, 2019 07:22
Show Gist options
  • Save jonmaim/49c02f742deed9002bb78ba09583a1d4 to your computer and use it in GitHub Desktop.
Save jonmaim/49c02f742deed9002bb78ba09583a1d4 to your computer and use it in GitHub Desktop.
Mongo shell: iterate over all dbs
/* switch to admin database and get list of databases */
db = db.getSiblingDB("admin");
dbs = db.runCommand({ "listDatabases": 1 }).databases;
/* iterate through each database and get its collections */
dbs.forEach(function(database) {
print(database.name);
db = db.getSiblingDB(database.name);
cols = db.getCollectionNames();
cols.forEach(function(col) {
if (col === 'emails') { print(col); }
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment