On this page本页内容
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()方法不接受任何参数。
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' ]
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()map defines a function that iterates over the list of databases. 定义一个在数据库列表上迭代的函数。Each iteration of map:map的每次迭代:
name variable,name变量,name using db.getSiblingDB(),db.getSiblingDB()连接到当前以name存储的数据库,db.getCollectionNames().db.getCollectionNames()返回当前数据库中的集合。