Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all databases in the mongo shell?

People also ask

Which of the following command can be used in mongo shell to find the current database name?

You can also use the db command to see your current database.

How do I list a collection in MongoDB?

Use the db. getCollectionNames() Method to List All Collections in the MongoDB Shell. The function db. getCollectionNames() returns an array with the names of all collections and views in the current database.

How do I select a database in MongoDB?

You can create new connections within the mongo shell. Open a new database connection. Open a connection to a new server using new Mongo() . Use getDB() method of the connection to select a database.


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.


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

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 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.