如何列出mongo shell中的所有数据库?

fra*_*acz 167 mongodb

我知道如何列出特定数据库中的所有集合,但是如何列出MongoDB shell中的所有可用数据库?

Rob*_*her 194

列出mongoDB控制台中的所有数据库都在使用该命令show dbs.

有关此内容的更多信息,请参阅可在mongo shell中使用的Mongo Shell Command Helpers.

  • 并且对于刚刚安装了mongodb的任何人(比如我)并且对于运行`db`显示当前数据库是`test`感到困惑,但这不是通过此页面上解释的任何命令列出的,http:// stackoverflow的.com/q /七万三千二百二十六分之三千八百七十二万六千三百一 (10认同)
  • 你到底怎么样才能达到这个目的:/ (3认同)
  • @JamieHutber,您可以通过在命令行中输入“ mongo”(而不是“ mongo --nodb”来不连接数据库)来获得shell (2认同)
  • 该命令不适用于“--eval”,仅适用于交互式 shell。这个答案的选项确实有效(尽管输出格式不同)/sf/answers/2253457741/ (2认同)

小智 51

对于MongoDB shell 3.0.5版,在shell中插入以下命令:

db.adminCommand('listDatabases')
Run Code Online (Sandbox Code Playgroud)

或者:

db.getMongo().getDBNames()
Run Code Online (Sandbox Code Playgroud)

  • 如果您在外壳中并且只想要名称:`mongo admin --quiet -u <mongodb_admin> -p [<password>] --eval'db.getMongo()。getDBNames()。forEach(function(db){ print(db)})'`hth (2认同)

小智 49

你也可以试试这个

对于数据库列表---

show databases
show dbs
Run Code Online (Sandbox Code Playgroud)

对于表/收藏清单---

show collections
show tables
db.getCollectionNames()
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助..


Sco*_*and 29

从命令行问题

mongo --quiet --eval  "printjson(db.adminCommand('listDatabases'))"
Run Code Online (Sandbox Code Playgroud)

它给出了输出

{
    "databases" : [
        {
            "name" : "admin",
            "sizeOnDisk" : 978944,
            "empty" : false
        },
        {
            "name" : "local",
            "sizeOnDisk" : 77824,
            "empty" : false
        },
        {
            "name" : "meteor",
            "sizeOnDisk" : 778240,
            "empty" : false
        }
    ],
    "totalSize" : 1835008,
    "ok" : 1
}
Run Code Online (Sandbox Code Playgroud)

  • 这里是自动化运行某些东西的最佳解决方案(无需先进入mongo shell模式) (2认同)

Dee*_*shy 10

有几个命令可以列出 MongoDB shell 中的所有数据库。

首先,使用“mongo”命令启动 Mongodb shell。

蒙戈

然后使用以下任意命令列出所有数据库。

  • 显示数据库
  • 显示数据库
  • db.adminCommand( { listDatabases: 1 , nameOnly : true} )

快照

欲了解更多详情,请查看此处

谢谢。


Ami*_*esh 9

在 shell 上列出 mongodb 数据库

 show databases     //Print a list of all available databases.
 show dbs   // Print a list of all databases on the server.
Run Code Online (Sandbox Code Playgroud)

更多基本命令

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.
Run Code Online (Sandbox Code Playgroud)