要在JavaScript数组中获取MongoDB数据库,可以使用runCommand()
。以下是查询以JavaScript数组获取MongoDB数据库的查询
> use admin; switched to db admin > allDatabasesDetails = db.runCommand({listDatabases: 1});
这将产生以下输出
{ "databases" : [ { "name" : "admin", "sizeOnDisk" : 847872, "empty" : false }, { "name" : "config", "sizeOnDisk" : 98304, "empty" : false }, { "name" : "local", "sizeOnDisk" : 73728, "empty" : false }, { "name" : "sample", "sizeOnDisk" : 1273856, "empty" : false }, { "name" : "sampleDemo", "sizeOnDisk" : 352256, "empty" : false }, { "name" : "studentSearch", "sizeOnDisk" : 262144, "empty" : false }, { "name" : "test", "sizeOnDisk" : 9527296, "empty" : false } ], "totalSize" : 12435456, "ok" : 1 }
以下是获取数据库总数的查询:
> allDatabaseName = [] [ ] > for (var j in allDatabasesDetails.databases) { allDatabaseName.push(dbs.databases[j].name) }
这将产生以下输出
7