We have a system with many different mongo databases. I regularly want to write ad-hoc queries that will apply to all (or a subset) of them, without having a priori knowledge of what databases are there.
I can do show dbs, which will visually print a list, but is there a way to do something like:
var db_list = listDatabases();
for (i = 0; i < db_list.length; i++) {
do_something(db_list[i])
}
My problem with show dbs is that it doesn’t capture any return values, so I can’t do anything productive with the output.
You can use the
'listDatabases'admin command for that:That returns an object that looks like this: