You would do it like this:
MongoClient mongoClient = new MongoClient();
List<String> dbs = mongoClient.getDatabaseNames();
This will just give you a list of all available database names.
You can see the documentation here .
Update:
As @CydrickT mentioned below, is getDatabaseNamesalready deprecated, so we need to switch to:
MongoClient mongoClient = new MongoClient();
MongoCursor<String> dbsCursor = mongoClient.listDatabaseNames().iterator();
while(dbsCursor.hasNext()) {
System.out.println(dbsCursor.next());
}
source
share