Database 如何列出mongo shell中的所有数据库?
我知道如何列出MongoDB shell中的所有可用数据库?在MongoDB控制台中列出所有数据库是使用命令Database 如何列出mongo shell中的所有数据库?,database,mongodb,mongodb-query,Database,Mongodb,Mongodb Query,我知道如何列出MongoDB shell中的所有可用数据库?在MongoDB控制台中列出所有数据库是使用命令show dbs 有关这方面的详细信息,请参阅可在mongo shell中使用的。对于MongoDB shell 3.0.5版,请在shell中插入以下命令: db.adminCommand('listDatabases') 或者: db.getMongo().getDBNames() 有关数据库列表: show databases show dbs 对于表格/集合列表: show
show dbs
有关这方面的详细信息,请参阅可在mongo shell中使用的。对于MongoDB shell 3.0.5版,请在shell中插入以下命令:
db.adminCommand('listDatabases')
或者:
db.getMongo().getDBNames()
有关数据库列表:
show databases
show dbs
对于表格/集合列表:
show collections
show tables
db.getCollectionNames()
从命令行问题
mongo --quiet --eval "printjson(db.adminCommand('listDatabases'))"
它给出了输出
{
"databases" : [
{
"name" : "admin",
"sizeOnDisk" : 978944,
"empty" : false
},
{
"name" : "local",
"sizeOnDisk" : 77824,
"empty" : false
},
{
"name" : "meteor",
"sizeOnDisk" : 778240,
"empty" : false
}
],
"totalSize" : 1835008,
"ok" : 1
}
在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.
使用//将当前数据库切换到。mongo shell变量db设置为当前数据库。
显示集合//打印当前数据库的所有集合的列表。
显示用户//打印当前数据库的用户列表。
显示角色//打印当前数据库的所有角色的列表,包括用户定义的和内置的。
我找到了一个解决方案,其中admin()/其他解决方案不起作用。
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()
有两个命令列出MongoDB shell中的所有DB 首先,使用“mongo”命令启动mongodbshell 蒙戈 然后使用以下任何命令列出所有数据库
- 放映星展
- 显示数据库
- adminCommand({listDatabases:1,nameOnly:true})
谢谢你。还有像我这样的人刚刚安装了mongodb的用户感到困惑的是,运行
db
显示当前数据库是test
,但这并没有通过本页上的任何ComnMand列出,这里解释了您到底是如何到达shall的:/@JamieHutber您通过在命令行上键入mongo
来获得shell的(属于mongo--nodb
以不连接到数据库)是的,我必须来这里做一些简单的事情,比如show dbs
,因为我在文档中找不到show dbs
命令。文档有时会很令人沮丧。这个命令在--eval
中不起作用,只是在一个交互式shell上。这个答案的选项确实起作用(尽管输出格式不同)如果您在shell中并且只需要名称,那么这里是运行自动化程序的最佳解决方案(无需先进入mongo shell模式):mongo admin--quiet-u-p[]--eval'db.getMongo().getDBNames().forEach(函数(db){print(db)})
hth