Database Manual / Reference / mongosh Methods / Connections

Mongo.getDBNames() (mongosh method方法)

Description描述

Mongo.getDBNames()

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

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

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

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. Mongo.getDBNames() returns:db.getMongo()方法创建到实例的连接。Mongo.getDBNames()返回:

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