我知道如何列出一个特定的数据库中的所有集合,但我如何列出MongoDB shell中所有可用的数据库?
当前回答
使用show dbs命令列出mongoDB控制台中所有的数据库。
有关mongo shell命令的更多信息,请参阅mongo shell快速参考。
其他回答
使用show dbs命令列出mongoDB控制台中所有的数据库。
有关mongo shell命令的更多信息,请参阅mongo shell快速参考。
对于MongoDB shell 3.0.5版本,在shell中插入以下命令:
db.adminCommand('listDatabases')
或者:
db.getMongo().getDBNames()
有几个命令列出MongoDB shell中的所有dbs。
首先,使用“mongo”命令启动Mongodb shell。
·
然后使用下列任意命令列出所有db。
show dbs 显示数据库 db。adminCommand({listDatabases: 1, nameOnly: true})
详情请点击这里
谢谢你!
我已经找到了一个解决方案,其中admin()/others不起作用。
const { promisify } = require('util');
const exec = promisify(require('child_process').exec)
async function test() {
var res = await exec('mongo --eval "db.adminCommand( { listDatabases: 1 }
)" --quiet')
return { res }
}
test()
.then(resp => {
console.log('All dbs', JSON.parse(resp.res.stdout).databases)
})
test()
在shell上列出mongodb数据库
show databases //Print a list of all available databases.
show dbs // Print a list of all databases on the server.
更多基本命令
use <db> // Switch current database to <db>. The mongo shell variable db is set to the current database.
show collections //Print a list of all collections for current database.
show users //Print a list of users for current database.
show roles //Print a list of all roles, both user-defined and built-in, for the current database.