328

I know how to list all collections in a particular database, but how do I list all available databases in MongoDB shell?

Naman
  • 27,789
  • 26
  • 218
  • 353
fracz
  • 20,536
  • 18
  • 103
  • 149

8 Answers8

431

Listing all the databases in mongoDB console is using the command show dbs.

For more information on mongo shell commands, refer the Mongo Shell Quick Reference.

Amol
  • 1,084
  • 10
  • 20
Robert Christopher
  • 4,940
  • 1
  • 20
  • 21
  • 36
    And for anyone (like me) that has just installed mongodb and are confused that running `db` shows the current database is `test` but this is not listed via any of the comnmands on this page that is explained here http://stackoverflow.com/q/38726310/73226 – Martin Smith Aug 07 '16 at 16:39
  • 3
    How on earth do you get to the shall though :/ – Jamie Hutber Aug 13 '17 at 23:52
  • 3
    @JamieHutber you get so shell by typing `mongo` on the command line (of `mongo --nodb` to not connect to a database) – magikMaker Jan 26 '18 at 13:18
  • 1
    Yeah I had to come here for something as simple as `show dbs` because I when I went to the docs I simply could not find the `show dbs` command anywhere. The 'docs' can be pretty frustrating at times. – MadHatter Jul 14 '18 at 16:17
  • 2
    That command does not work in a `--eval`, just on an interactive shell. This answer's options does work (output format is different though) https://stackoverflow.com/a/32192253/1837991 – Gert van den Berg Nov 11 '19 at 08:11
  • I get this when I run in MongoDB Pod terminal in Red Hat OpenShift: `"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0, $db: \"admin\" }"` – user1063287 Jan 16 '23 at 09:33
93

For database list:

show databases
show dbs

For table/collection list:

show collections
show tables
db.getCollectionNames()
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Amol Udage
  • 2,917
  • 19
  • 27
66

For MongoDB shell version 3.0.5 insert the following command in the shell:

db.adminCommand('listDatabases')

or alternatively:

db.getMongo().getDBNames()
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • 5
    if you are in your shell and only want names: `mongo admin --quiet -u -p [] --eval 'db.getMongo().getDBNames().forEach(function(db){print(db)})'` hth – Boop Jun 06 '19 at 12:55
48

From the command line issue

mongo --quiet --eval  "printjson(db.adminCommand('listDatabases'))"

which gives output

{
    "databases" : [
        {
            "name" : "admin",
            "sizeOnDisk" : 978944,
            "empty" : false
        },
        {
            "name" : "local",
            "sizeOnDisk" : 77824,
            "empty" : false
        },
        {
            "name" : "meteor",
            "sizeOnDisk" : 778240,
            "empty" : false
        }
    ],
    "totalSize" : 1835008,
    "ok" : 1
}

to obtain a vertical list of all databases for downstream processing do this

mongo --quiet --eval  "printjson(db.adminCommand('listDatabases'))" | jq  '.databases[].name' | tr -d '"' 

which gives below output listing all databases

admin
local
meteor
Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
  • 2
    Best solution here for running something automated (without going into mongo shell mode first) – herm Feb 21 '19 at 14:16
  • Based on this answer I came up with `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'` to list all databases, sorted by size and the total at the end. – Paul Tobias Aug 30 '22 at 02:55
19

To list mongodb database on shell

 show databases     //Print a list of all available databases.
 show dbs   // Print a list of all databases on the server.

Few more basic commands

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.
Amitesh Bharti
  • 14,264
  • 6
  • 62
  • 62
10

Couple of commands are there to list all dbs in MongoDB shell.

first , launch Mongodb shell using 'mongo' command.

mongo

Then use any of the below commands to list all the DBs.

  • show dbs
  • show databases
  • db.adminCommand( { listDatabases: 1 , nameOnly : true} )

A snap shot

For more details please check here

Thank you.

Deepak Koshy
  • 230
  • 3
  • 9
2

According to MongoDB official document, for MongoDB 4+, you can list database name only by running db.adminCommand( { listDatabases: 1, , nameOnly: true } ) against the admin database.

If you are using MongoDB Cloud, you need to connect to your MongoDB deployment first. In that case, you can run this command mongosh "mongodb+srv://cluster0.<your-connection-string>.mongodb.net" --apiVersion 1 --username <your-user-name> in your terminal.

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 })
}
Henry S.
  • 432
  • 1
  • 3
  • 8
1

I have found one solution, where admin()/others didn't worked.

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()
Rohit Parte
  • 3,365
  • 26
  • 26