How to List All Databases in the Mongo Shell?

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

0

8 Answers

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

For more information on this, refer the Mongo Shell Command Helpers that can be used in the mongo shell.

5

For database list:

show databases
show dbs

For table/collection list:

show collections
show tables
db.getCollectionNames()

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

db.adminCommand('listDatabases')

or alternatively:

db.getMongo().getDBNames()
1

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
}
1

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.

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} )

For more details please check here

Thank you.

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()

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 })
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest