我知道如何list all collections in a particular database,但是如何在 MongoDB shell 中列出所有可用的数据库?
对于数据库列表:
show databases
show dbs
对于表/集合列表:
show collections
show tables
db.getCollectionNames()
对于 MongoDB shell 版本 3.0.5,在 shell 中插入以下命令:
db.adminCommand('listDatabases')
或者:
db.getMongo().getDBNames()
mongo admin --quiet -u <mongodb_admin> -p [<password>] --eval 'db.getMongo().getDBNames().forEach(function(db){print(db)})'
hth
从命令行问题
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.
有几个命令可以列出 MongoDB shell 中的所有数据库。
首先,使用 'mongo' 命令启动 Mongodb shell。
蒙哥
然后使用以下任何命令列出所有数据库。
显示数据库
显示数据库
db.adminCommand( { listDatabases: 1 , nameOnly : true} )
https://i.stack.imgur.com/hkzT4.png
有关详细信息,请查看here
谢谢你。
我找到了一种解决方案,其中 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()
根据 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 }) }
不定期副业成功案例分享
db
显示当前数据库为test
感到困惑的任何人(如我),但这并未通过此页面上的任何命令列出,此处解释为 stackoverflow.com/q/38726310/73226mongo
来获得如此外壳(mongo --nodb
以不连接到数据库)show dbs
这样简单的东西,因为当我去文档时,我根本无法在任何地方找到show dbs
命令。 “文档”有时会让人非常沮丧。--eval
中不起作用,只能在交互式 shell 中使用。此答案的选项确实有效(但输出格式不同)stackoverflow.com/a/32192253/1837991