Mongo.getDBNames()
On this page本页内容
Description描述
Mongo.getDBNames()
-
Returns a list of available databases.返回可用数据库的列表。Mongo.getDBNames()
calls thelistDatabases
command.Mongo.getDBNames()
调用listDatabases
命令。TheMongo.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. 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 ofmap
:map
的每次迭代:assigns a database to the将数据库分配给name
variable,name
变量,connects to the database currently stored in使用name
usingdb.getSiblingDB()
,db.getSiblingDB()
连接到当前以name
存储的数据库,returns the collections in the current database using使用db.getCollectionNames()
.db.getCollectionNames()
返回当前数据库中的集合。