我知道如何在特定数据库中列出所有集合,但是如何在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)})'
希望能对你有所帮助。 - Boop从命令行发出命令
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 --quiet --eval "printjson(db.adminCommand('listDatabases'))" | jq '.databases[].name' | tr -d '"'
这将输出以下内容,列出所有数据库
admin
local
meteor
listDatabases="$(mongo --quiet --eval "printjson(db.adminCommand('listDatabases'))")"; ( while IFS=$'\t' read -r size name; do printf '%s\t%s\n' "$name" "$(numfmt --to=iec <<<"$size")"; done < <(jq -r '.databases[] | [.sizeOnDisk, .name] | @tsv' <<<"$listDatabases" | sort -rn); printf -- '-\t-\n'; printf "TOTAL\t%s\n" "$(jq -r .totalSize <<<"$listDatabases" | numfmt --to=iec)" ) | column -t -s$'\t'
。 - Paul Tobias列出MongoDB数据库的Shell命令
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。
mongo
然后使用以下任一命令来列出所有数据库。
有关更多详细信息,请查看此处。
谢谢。
db.adminCommand( { listDatabases: 1, , nameOnly: true } )
来列出数据库名称。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' },
{ name: 'sample_guides' },
{ name: 'sample_mflix' },
{ name: 'sample_restaurants' },
{ name: 'sample_supplies' },
{ name: 'sample_training' },
{ name: '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 })
}
我找到了一个解决方案,其中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()
db
命令后显示当前数据库是test
,但在本页面的任何命令中都找不到它。这个问题在这里得到了解释:https://dev59.com/hlkT5IYBdhLWcg3wWONA - Martin Smithmongo
(或在mongo --nodb
中不连接到数据库)会让你感到很迷茫。 - magikMakershow dbs
这样简单的命令,因为当我去文档中查找时,根本找不到show dbs
命令。有时候,“文档”真的很让人沮丧。 - MadHatter--eval
中不起作用,只能在交互式 shell 中使用。这个答案的选项确实可行(输出格式不同)。https://dev59.com/8F8e5IYBdhLWcg3wPIaA#32192253 - Gert van den Berg