Mongo.getDBNames()
説明
Mongo.getDBNames()
Returns a list of available databases.
Mongo.getDBNames()
calls thelistDatabases
command.Mongo.getDBNames()
メソッドはパラメータを取ります 。
互換性
このメソッドは、次の環境でホストされている配置で使用できます。
MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
例
データベースを一覧表示する
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' ]
Map Database List to Another Method
使用 Mongo.getDBNames()
to get a list of collections:
db.getMongo().getDBNames().map( name => db.getSiblingDB( name ).getCollectionNames() )
出力例:
[ [ '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
:assigns a database to the
name
variable,connects to the database currently stored in
name
usingdb.getSiblingDB()
,returns the collections in the current database using
db.getCollectionNames()
.