mongodb 如何列出mongo shell中的所有数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25947929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to list all databases in the mongo shell?
提问by fracz
I know how to list all collections in a particular database, but how do I list all available databases in MongoDB shell?
我知道如何列出特定数据库中的所有集合,但如何在 MongoDB shell 中列出所有可用数据库?
回答by Robert Christopher
Listing all the databases in mongoDB console is using the command show dbs
.
使用命令在 mongoDB 控制台中列出所有数据库show dbs
。
For more information on this, refer the Mongo Shell Command Helpersthat can be used in the mongo shell.
有关这方面的更多信息,请参阅可在 mongo shell 中使用的Mongo Shell 命令帮助程序。
回答by Carlos F. Enguix
For MongoDB shell version 3.0.5 insert the following command in the shell:
对于 MongoDB shell 版本 3.0.5,在 shell 中插入以下命令:
db.adminCommand('listDatabases')
or alternatively:
或者:
db.getMongo().getDBNames()
回答by Amol Udage
You can also try this
你也可以试试这个
For database list---
对于数据库列表---
show databases
show dbs
For table/collection list ---
对于表/集合列表 ---
show collections
show tables
db.getCollectionNames()
Hope this helps..
希望这可以帮助..
回答by Scott Stensland
From the command line issue
从命令行问题
mongo --quiet --eval "printjson(db.adminCommand('listDatabases'))"
which gives output
这给出了输出
{
"databases" : [
{
"name" : "admin",
"sizeOnDisk" : 978944,
"empty" : false
},
{
"name" : "local",
"sizeOnDisk" : 77824,
"empty" : false
},
{
"name" : "meteor",
"sizeOnDisk" : 778240,
"empty" : false
}
],
"totalSize" : 1835008,
"ok" : 1
}
回答by Amitesh
To list mongodb database on shell
在 shell 上列出 mongodb 数据库
show databases //Print a list of all available databases.
show dbs // Print a list of all databases on the server.
Few more basic commands
更多基本命令
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.
回答by Rohit Parte
I have found one solution, where admin()/others didn't worked.
我找到了一种解决方案,其中 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()