Ich kann alle Sammlungen in einer bestimmten Datenbank auflisten , aber wie liste ich alle verfügbaren Datenbanken in der MongoDB-Shell auf?
Die Auflistung aller Datenbanken in der MongoDB-Konsole verwendet den Befehl show dbs
.
Weitere Informationen hierzu finden Sie in den Mongo Shell Command Helpers , die in der Mongo Shell verwendet werden können.
Fügen Sie für MongoDB Shell Version 3.0.5 den folgenden Befehl in die Shell ein:
db.adminCommand('listDatabases')
oder alternativ:
db.getMongo().getDBNames()
Sie können dies auch versuchen
Für Datenbankliste ---
show databases
show dbs
Für Tisch/Sammelliste ---
show collections
show tables
db.getCollectionNames()
Hoffe das hilft..
Von der Befehlszeile aus
mongo --quiet --eval "printjson(db.adminCommand('listDatabases'))"
was gibt Ausgabe
{
"databases" : [
{
"name" : "admin",
"sizeOnDisk" : 978944,
"empty" : false
},
{
"name" : "local",
"sizeOnDisk" : 77824,
"empty" : false
},
{
"name" : "meteor",
"sizeOnDisk" : 778240,
"empty" : false
}
],
"totalSize" : 1835008,
"ok" : 1
}
Ich habe eine Lösung gefunden, wo admin ()/andere nicht funktioniert haben.
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()
Auflisten der Mongodb-Datenbank auf Shell
show databases //Print a list of all available databases.
show dbs // Print a list of all databases on the server.
Einige grundlegende Befehle
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.