Reputation: 7715
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.
Upvotes: 12
Views: 8905
Reputation: 335
There is also getDBNames() (i like how JohnnyHK's answer gets the size.
d = db.getMongo().getDBNames()
[ "graylog2", "local", "admin", "test" ]
Then I can:
for (var x in d) { db = new Mongo().getDB(d[x]); print(db); y = print(db.getCollectionNames()); }
Upvotes: 1
Reputation: 311865
You can use the 'listDatabases'
admin command for that:
var db_list = db.adminCommand('listDatabases');
That returns an object that looks like this:
{
"databases" : [
{
"name" : "test",
"sizeOnDisk" : 2097152000,
"empty" : false
},
{
"name" : "local",
"sizeOnDisk" : 1,
"empty" : true
}
],
"totalSize" : 8487174144,
"ok" : 1
}
Upvotes: 27