- Reference >
- Database Commands >
- Administration Commands >
- listDatabases
listDatabases¶
On this page
Definition¶
-
listDatabases¶ The
listDatabasescommand provides a list of all existing databases along with basic statistics about them. ThelistDatabasesmust run against theadmindatabase, as in the following example:The value (e.g.
1) does not affect the output of the command.The
listDatabasescommand can take the following optional fields:Field Type Description filterdocument 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:namesizeOnDiskemptyshards
nameOnlyboolean 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, solistDatabasesreturns the name and size information of each database.authorizedDatabasesboolean Optional. A flag that determines which databases are returned based on the user privileges when access control is enabled.
- If
authorizedDatabasesis unspecified, and- If the user has
listDatabasesaction on the cluster resource,listDatabasescommand returns all databases. - If the user does not have
listDatabasesaction on the cluster:- For MongoDB 4.0.6+,
listDatabasescommand 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,
listDatabasescommand returns only the databases for which the user has thefindaction on the database resource (and not the collection resource).
- For MongoDB 4.0.6+,
- If the user has
- If
authorizedDatabasesistrue,- For MongoDB 4.0.6+,
listDatabasescommand 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,
listDatabasescommand returns only the databases for which the user has thefindaction on the database resource (and not the collection resource).
- For MongoDB 4.0.6+,
- If
authorizedDatabasesisfalse, and- If the user has
listDatabasesaction on the cluster,listDatabasescommand returns all databases - If the user does not have
listDatabasesaction on the cluster,listDatabasescommand errors with insufficient permissions.
- If the user has
For more information, see Behavior.
New in version 4.0.5.
commentany Optional. A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:
- mongod log messages, in the
attr.command.cursor.commentfield. - Database profiler output, in the
command.commentfield. currentOpoutput, in thecommand.commentfield.
A comment can be any valid BSON type (string, integer, object, array, etc).
New in version 4.4.
Behavior¶
When authentication is enabled:
Note
For mongosh connected to earlier
versions of MongoDB deployment (e.g. 3.6.10),
- If the user has
listDatabasesaction on the cluster resource,show dbsreturns all databases. - If the user does not have
listDatabasesaction on the cluster resource,show dbsreturns only the databases for which the user has privileges (including those databases for which the user has privileges on specific collections).
- MongoDB 4.0.6+
- MongoDB 4.0.5
- MongoDB 4.0.0-4.0.4
For MongoDB 4.0.6+, the listDatabases command
returns different values based on the privileges assigned to
the user who executes the command and the
authorizedDatabases command option:
- If
authorizedDatabasesis unspecified, and- If the user has
listDatabasesaction on the cluster resource,listDatabasescommand returns all databases. - If the user does not have
listDatabasesaction on the cluster,listDatabasescommand returns only the databases for which the user has privileges (including databases for which the user has privileges on specific collections).
- If the user has
- If
authorizedDatabasesistrue,listDatabasescommand returns only the databases for which the user has privileges (including databases for which the user has privileges on specific collections). - If
authorizedDatabasesisfalse, and- If the user has
listDatabasesaction on the cluster,listDatabasescommand returns all databases - If the user does not have
listDatabasesaction on the cluster,listDatabasescommand errors with insufficient permissions.
- If the user has
For MongoDB 4.0.5, the listDatabases command
returns different values based on the privileges assigned to
the user who executes the command and the
authorizedDatabases command option:
- If
authorizedDatabasesis unspecified, and- If the user has
listDatabasesaction on the cluster resource,listDatabasescommand returns all databases. - If the user does not have
listDatabasesaction on the cluster,listDatabasescommand returns only the databases for which the user has thefindaction on the database resource (and not the collection resource).
- If the user has
- If
authorizedDatabasesistrue,listDatabasescommand returns only the databases for which the user has thefindaction on the database resource (and not the collection resource). - If
authorizedDatabasesisfalse, and- If the user has
listDatabasesaction on the cluster,listDatabasescommand returns all databases - If the user does not have
listDatabasesaction on the cluster,listDatabasescommand errors with insufficient permissions.
- If the user has
For MongoDB 4.0.0-4.0.4, the listDatabases command
returns different values based on the privileges assigned to
the user who executes the command.
- If the user has the
listDatabasesprivilege action on the cluster, thelistDatabasescommand returns a list of all existing databases. - If the user does not have the
listDatabasesprivilege action on the cluster, thelistDatabasescommand only returns a list of databases for which the user has thefindaction.
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.
Replica Set Member State Restriction¶
Starting in MongoDB 4.4, to run on a replica set member,
listDatabases operations require the member to be in
PRIMARY or SECONDARY state. If the member
is in another state, such as STARTUP2, the
operation errors.
In previous versions, the operations also run when the member
is in STARTUP2. The operations wait until the member
transitioned to RECOVERING.
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¶
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¶
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
shardsembedded document to each database’s summary document ifnameOnly: false, and - excludes the
localdatabase.
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 and indexes.
For example:
See also
Output¶
-
listDatabases.databases¶ Type: Array
Array of documents, each containing information on a single database.
-
listDatabases.databases.name¶ Type: String
Name of the database.
-
listDatabases.databases.sizeOnDisk¶ Type: Integer
Total size of the database files on disk, expressed in bytes.
-
listDatabases.databases.empty¶ Type: Boolean
Specifies whether the database is empty.
-
listDatabases.databases.shards¶ Type: Document
Each element in the
shardsdocument 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.shardsonly appears in the output ifnameOnly: false.See Sharded Clusters for details.
-
listDatabases.totalSize¶ Type: Integer
Sum of all the
sizeOnDiskfields in bytes.
-
listDatabases.totalSizeMb¶ Type: Integer
Sum of all the
sizeOnDiskfields, expressed in megabytes.
-
listDatabases.ok¶ Type: Integer
Return value for the command. A value of
1indicates success.