How to list all databases in the mongo shell?

DatabaseMongodbMongodb Query

Database Problem Overview


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

Database Solutions


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

Solution 2 - Database

For database list:

show databases
show dbs

For table/collection list:

show collections
show tables
db.getCollectionNames()

Solution 3 - Database

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

db.adminCommand('listDatabases')

or alternatively:

db.getMongo().getDBNames()

Solution 4 - Database

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
}

Solution 5 - Database

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.

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

A snap shot

For more details please check here

Thank you.

Solution 7 - Database

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfraczView Question on Stackoverflow
Solution 1 - DatabaseRobert ChristopherView Answer on Stackoverflow
Solution 2 - DatabaseAmol UdageView Answer on Stackoverflow
Solution 3 - DatabaseCarlos F. EnguixView Answer on Stackoverflow
Solution 4 - DatabaseScott StenslandView Answer on Stackoverflow
Solution 5 - DatabaseAmitesh BhartiView Answer on Stackoverflow
Solution 6 - DatabaseDeepak KoshyView Answer on Stackoverflow
Solution 7 - DatabaseRohit ParteView Answer on Stackoverflow