ChatGPT解决这个技术问题 Extra ChatGPT

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

我知道如何list all collections in a particular database,但是如何在 MongoDB shell 中列出所有可用的数据库?


R
Robert Christopher

使用命令 show dbs 在 mongoDB 控制台中列出所有数据库。

有关这方面的更多信息,请参阅可在 mongo shell 中使用的 Mongo Shell Command Helpers


对于刚刚安装 mongodb 并且对运行 db 显示当前数据库为 test 感到困惑的任何人(如我),但这并未通过此页面上的任何命令列出,此处解释为 stackoverflow.com/q/38726310/73226
不过,你到底是怎么做到的:/
@JamieHutber 您可以通过在命令行上输入 mongo 来获得如此外壳(mongo --nodb 以不连接到数据库)
是的,我必须来这里是为了像 show dbs 这样简单的东西,因为当我去文档时,我根本无法在任何地方找到 show dbs 命令。 “文档”有时会让人非常沮丧。
该命令在 --eval 中不起作用,只能在交互式 shell 中使用。此答案的选项确实有效(但输出格式不同)stackoverflow.com/a/32192253/1837991
W
Willi Mentzel

对于数据库列表:

show databases
show dbs

对于表/集合列表:

show collections
show tables
db.getCollectionNames()

J
JohnnyHK

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

db.adminCommand('listDatabases')

或者:

db.getMongo().getDBNames()

如果你在你的 shell 中并且只想要名字:mongo admin --quiet -u <mongodb_admin> -p [<password>] --eval 'db.getMongo().getDBNames().forEach(function(db){print(db)})' hth
S
Scott Stensland

从命令行问题

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
}

此处用于运行自动化的最佳解决方案(无需先进入 mongo shell 模式)
A
Amitesh Bharti

在 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.

D
Deepak Koshy

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

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

蒙哥

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

显示数据库

显示数据库

db.adminCommand( { listDatabases: 1 , nameOnly : true} )

https://i.stack.imgur.com/hkzT4.png

有关详细信息,请查看here

谢谢你。


R
Rohit Parte

我找到了一种解决方案,其中 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()

H
Henry S.

根据 MongoDB official document,对于 MongoDB 4+,您只能通过对 admin 数据库运行 db.adminCommand( { listDatabases: 1, , nameOnly: true } ) 来列出数据库名称。

如果您使用的是 MongoDB Cloud,则需要先连接到您的 MongoDB 部署。在这种情况下,您可以在终端中运行此命令 mongosh "mongodb+srv://cluster0.<your-connection-string>.mongodb.net" --apiVersion 1 --username <your-user-name>

Atlas atlas-xxxxxx-shard-0 [primary] test> db.adminCommand({listDatabases:1 , nameOnly: true}) { databases: [ { name: 'sample_airbnb' }, { name: 'sample_analytics' }, { name: 'sample_geospatial'},{名称:'sample_guides'},{名称:'sample_mflix'},{名称:'sample_restaurants'},{名称:'sample_supplies'},{名称:'sample_training'},{名称:'sample_weatherdata ' }, { name: 'admin' }, { name: 'local' } ], ok: 1, '$clusterTime': { clusterTime: Timestamp({ t: xxxxxxxxxx, i: 1 }), signature: { hash: Binary(Buffer.from("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "hex"), 0), keyId: Long("xxxxxxxxxxxxx") } }, operationTime: Timestamp({ t: xxxxxxxxxx, i: 1 }) }