- Reference >
- Database Commands >
- Administration Commands >
- listDatabases
listDatabases¶
On this page
Definition¶
-
listDatabases
¶ The
listDatabases
command provides a list of all existing databases along with basic statistics about them. ThelistDatabases
must run against theadmin
database, as in the following example:The value (e.g.
1
) does not affect the output of the command.The
listDatabases
command can take the following optional fields:Field Type Description filter
document Optional. A query predicate that determines which databases are listed.
You can specify a condition on any of the fields in the output of
listDatabases
:name
sizeOnDisk
empty
shards
nameOnly
boolean Optional. A flag to indicate whether the command should return just the database names, or return both database names and size information.
Returning size information requires locking each database one at a time, while returning only names does not require locking any database.
The default value is
false
, solistDatabases
returns the name and size information of each database.authorizedDatabases
boolean Optional. A flag that determines which databases are returned based on the user privileges when access control is enabled.
- If
authorizedDatabases
is unspecified, and- If the user has
listDatabases
action on the cluster resource,listDatabases
command returns all databases. - If the user does not have
listDatabases
action on the cluster:- For MongoDB 4.0.6+,
listDatabases
command returns only the databases for which the user has privileges (including databases for which the user has privileges on specific collections). - For MongoDB 4.0.5,
listDatabases
command returns only the databases for which the user has thefind
action on the database resource (and not the collection resource).
- For MongoDB 4.0.6+,
- If the user has
- If
authorizedDatabases
istrue
,- For MongoDB 4.0.6+,
listDatabases
command returns only the databases for which the user has privileges (including databases for which the user has privileges on specific collections). - For MongoDB 4.0.5,
listDatabases
command returns only the databases for which the user has thefind
action on the database resource (and not the collection resource).
- For MongoDB 4.0.6+,
- If
authorizedDatabases
isfalse
, and- If the user has
listDatabases
action on the cluster,listDatabases
command returns all databases - If the user does not have
listDatabases
action on the cluster,listDatabases
command errors with insufficient permissions.
- If the user has
For more information, see Behavior.
Behavior¶
When authentication is enabled,
the listDatabases
command returns different values based on
the privileges assigned to the user who executes the command and the
authorizedDatabases
command option:
show dbs
For mongo
shell version 4.0.6+ connected to earlier
versions of MongoDB deployment (e.g. 3.6.10),
- If the user has
listDatabases
action on the cluster resource,show dbs
returns all databases.
Client Disconnection¶
Starting in MongoDB 4.2, if the client that issued listDatabases
disconnects before the operation completes, MongoDB marks listDatabases
for termination using killOp
.
Examples¶
List Database Names and Sizes¶
Run listDatabases
against the admin
database:
The following is an example of a listDatabases
result:
List Database Names Only¶
New in version 3.6.
Run listDatabases
against the admin
database. Specify
the nameOnly: true
option:
The following is an example of a listDatabases
results
when run with the nameOnly: true
option:
List Databases That Match the Filter¶
New in version 3.6.
Run listDatabases
against the admin
database. Specify
the filter
option to only list databases that match the specified filter criteria.
For example, the following specifies a filter such that
listDatabases
only returns information on databases whose
name matches the specified regular expression
:
Sharded Clusters¶
When executed against a mongos
instance,
listDatabases
:
- adds a
shards
embedded document to each database’s summary document ifnameOnly: false
, and - excludes the
local
database.
Each element in the shards
embedded document consists of a field
whose key gives the name of a collection on that shard, and whose value
represents the collection’s size in bytes.
The sizeOnDisk
field represents the total size of all
listed collections.
For example:
See also