Docs Menu
Docs Home
/
MongoDB Manual
/ / /

Mongo.getDBNames()

On this page

  • Description
  • Compatibility
  • Examples
Mongo.getDBNames()

Returns a list of available databases. Mongo.getDBNames() calls the listDatabases command.

The Mongo.getDBNames() method doesn't take any parameters.

This method is available in deployments hosted in the following environments:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

List the available databases for the current MongoDB instance:

db.getMongo().getDBNames()

The db.getMongo() method creates a connection to the instance. Mongo.getDBNames() returns:

[ 'admin', 'config', 'local', 'test' ]

Use Mongo.getDBNames() to get a list of collections:

db.getMongo().getDBNames().map(
name => db.getSiblingDB( name ).getCollectionNames()
)

Example output:

[
[ 'system.users', 'system.keys', 'system.version' ],
[
'settings',
'tenantMigrationRecipients',
'system.sessions',
'transactions',
'external_validation_keys',
'image_collection',
'tenantMigrationDonors',
'system.indexBuilds'
],
[
'replset.minvalid',
'system.views',
'oplog.rs',
'replset.initialSyncId',
'startup_log',
'system.replset',
'system.rollback.id',
'replset.oplogTruncateAfterPoint',
'replset.election',
'system.tenantMigration.oplogView'
],
[
'feedback',
'inventory',
'engineers',
'clothes'
]
]
  • Mongo.getDBNames() returns a list of databases.

  • map defines a function that iterates over the list of databases. Each iteration of map:

Back

Mongo.getDB