How to show databases in MongoDB
List MongoDB databases with show dbs or listDatabases, and why a database shows up empty, never disappears, or is hidden from your user.
Quick Answer
In mongosh, run show dbs (or its alias show databases):
show dbs
For an array of names in a script, use db.getMongo().getDBNames().
List databases in mongosh
show dbs prints every database the server has, with its size on disk:
show dbs
// admin 40.00 KiB
// config 12.00 KiB
// local 40.00 KiB
// shop 8.00 KiB
admin, config and local are system databases that every server has. Leave them alone unless you know why you're in there.
Three ways to get the list, depending on what you need back:
| Command | Returns | Use it when |
|---|---|---|
show dbs | Printed names and sizes | You are looking at the shell |
db.getMongo().getDBNames() | An array of strings | You want to loop over names in a script |
db.adminCommand({ listDatabases: 1 }) | Documents with sizes, plus a total | You need sizes or a filter |
Get sizes, or filter by name
listDatabases is the command behind show dbs. Call it yourself to get the raw numbers or narrow the list:
// every database with sizeOnDisk, plus totalSize for the server
db.adminCommand({ listDatabases: 1 })
// names only: faster, and needs fewer privileges
db.adminCommand({ listDatabases: 1, nameOnly: true }).databases.map(d => d.name)
// only databases whose name starts with "tenant_"
db.adminCommand({ listDatabases: 1, filter: { name: /^tenant_/ } })
To print every database with its collections in one go:
db.getMongo().getDBNames().forEach(name => {
print('--', name)
db.getSiblingDB(name).getCollectionNames().forEach(c => print(' ', c))
})
What we found testing this
Most guides say an empty database doesn't show up. That's only half right. Here's what MongoDB 7.0 actually does:
1. A database with zero documents is listed
What counts is whether a collection exists, not whether it holds data. One empty collection is enough:
db.getSiblingDB('reports').createCollection('daily')
db.adminCommand({ listDatabases: 1, filter: { name: 'reports' } }).databases
// [ { name: 'reports', sizeOnDisk: 8192, empty: false } ]
2. A database that only ever had views or time series never goes away
Creating a view or a time series collection also creates a hidden system.views collection. Drop every view and collection afterwards and system.views stays behind, so the database keeps appearing in show dbs:
use metrics
db.createCollection('cpu', { timeseries: { timeField: 't' } })
db.cpu.drop()
db.getCollectionNames() // [ 'system.views' ]
show dbs // metrics is still listed
Only db.dropDatabase() removes it.
3. Restricted users only see databases that exist and that they can access
We logged in as a user with readWrite on shop. Another database, billing, existed on the server. show dbs returned only shop. A second user, with read on two databases nobody had created yet, got an empty list. Grants on a database don't make it appear; data does.
// as a user with read on two databases that don't exist yet
db.adminCommand({ listDatabases: 1 }).databases
// []
// asking for every database explicitly
db.adminCommand({ listDatabases: 1, authorizedDatabases: false })
// MongoServerError: Insufficient permissions to list all databasesFrom Node.js
With the official mongodb driver, ask the admin interface:
import { MongoClient } from 'mongodb'
const client = new MongoClient(process.env.MONGODB_URI)
await client.connect()
const { databases } = await client.db().admin().listDatabases({ nameOnly: true })
console.log(databases.map(d => d.name)) // [ 'admin', 'config', 'local', 'shop' ]
await client.close()
From Python (PyMongo)
from pymongo import MongoClient
client = MongoClient("mongodb://localhost:27017")
print(client.list_database_names()) # ['admin', 'config', 'local', 'shop']
for info in client.list_databases(): # documents with sizeOnDisk
print(info["name"], info["sizeOnDisk"])
Why your database is missing from the list
- It has no data yet.
use mydbonly switches the shell's context. MongoDB creates the database on the first write, so an unwritten database doesn't appear. See how to create a database. - Its last collection was dropped. A database with no collections stops existing, and disappears from
show dbs. The exception is a leftoversystem.views, covered above. - You're connected to a different server. Run
db.getMongo()to see the host you're on. A localmongodand an Atlas cluster can easily get mixed up. - Your user can't list databases. Without the
listDatabasesprivilege, the server returns only existing databases your user has privileges on.
show dbs rounds sizes and reports space on disk, not data size. A database that holds a few small documents still shows the minimum allocation, typically a few KiB.
Frequently Asked Questions
What is the difference between show dbs and show databases?
None. show databases is an alias for show dbs in mongosh and the legacy mongo shell.
Why does show dbs not list the database I just created with use?
use does not create anything. The database appears after the first document is inserted or the first collection is created.
How do I see the current database?
Run db in mongosh. It prints the name of the database you are using. A new connection starts in test unless the connection string names another database.
What are the admin, config and local databases?
System databases. admin holds users and roles defined there, config holds sharding and session metadata, and local holds the replication oplog. Do not store application data in them.