Mongo.getDBNames()

On this page本页内容

Description描述

Mongo.getDBNames()

Returns a list of available databases. 返回可用数据库的列表。Mongo.getDBNames() calls the listDatabases command.调用listDatabases命令。

The Mongo.getDBNames() method doesn't take any parameters.Mongo.getDBNames()方法不接受任何参数。

Examples示例

List Databases列出数据库

List the available databases for the current MongoDB instance:列出当前MongoDB实例的可用数据库:

db.getMongo().getDBNames()

The db.getMongo() method creates a connection to the instance. db.getMongo()方法创建到实例的连接。Mongo.getDBNames() returns:返回:

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

Map Database List to Another Method将数据库列表映射到其他方法

Use Mongo.getDBNames() to get a list of collections:使用Mongo.getDBNames()获取集合列表:

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:map的每次迭代:

←  Mongo.getDB()Mongo.getDBs() →