- Reference >
mongoshMethods >- Connection Methods >
- Mongo.getDBNames()
Mongo.getDBNames()¶
On this page
Description¶
-
Mongo.getDBNames()¶ Returns a list of available databases.
Mongo.getDBNames()calls thelistDatabasescommand.The
Mongo.getDBNames()method doesn’t take any parameters.
Examples¶
List Databases¶
List the available databases for the current MongoDB instance:
The db.getMongo() method creates a connection to the
instance. Mongo.getDBNames() returns:
Map Database List to Another Method¶
Use Mongo.getDBNames() to get a list of collections:
Example output:
Mongo.getDBNames()returns a list of databases.mapdefines a function that iterates over the list of databases. Each iteration ofmap:- assigns a database to the
namevariable, - connects to the database currently stored in
nameusingdb.getSiblingDB(), - returns the collections in the current database using
db.getCollectionNames().
- assigns a database to the