On this page本页内容
listDatabases
¶The listDatabases
command provides a list of all existing databases along with basic statistics about them. The listDatabases
must run against the admin
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:
filter |
document |
You can specify a condition on any of the fields in the output of
|
nameOnly |
boolean |
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
|
authorizedDatabases |
boolean |
For more information, see Behavior. New in version 4.0.5. |
comment |
any |
A comment can be any valid BSON type (string, integer, object, array, etc).
|
listDatabases
returns a document that contains:
databases
whose value is an array of documents, one document for each database. Each document contains:name
field with the database name.sizeOnDisk
field with the total size of the database files on disk in bytes.empty
field specifying whether the database has any data.shards
field that includes the shard and the size in bytes of the database on disk for each shard.totalSize
whose value is the sum of all the sizeOnDisk
fields in bytes.When authentication is enabled:
show dbs
For mongo
shell version 4.0.6+ connected to earlier versions of MongoDB deployment (e.g. 3.6.10),
listDatabases
action on the cluster resource, show dbs
returns all databases.listDatabases
action on the cluster resource, show dbs
returns only the databases for which the user has privileges (including those databases for which the user has privileges on specific collections).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:
authorizedDatabases
is unspecified, andlistDatabases
action on the cluster resource, listDatabases
command returns all databases.listDatabases
action on the cluster, listDatabases
command returns only the databases for which the user has privileges (including databases for which the user has privileges on specific collections).authorizedDatabases
is true
, listDatabases
command returns only the databases for which the user has privileges (including databases for which the user has privileges on specific collections).authorizedDatabases
is false
, andlistDatabases
action on the cluster, listDatabases
command returns all databaseslistDatabases
action on the cluster, listDatabases
command errors with insufficient permissions.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:
authorizedDatabases
is unspecified, andlistDatabases
action on the cluster resource, listDatabases
command returns all databases.listDatabases
action on the cluster, listDatabases
command returns only the databases for which the user has the find
action on the database resource (and not the collection resource).authorizedDatabases
is true
, listDatabases
command returns only the databases for which the user has the find
action on the database resource (and not the collection resource).authorizedDatabases
is false
, andlistDatabases
action on the cluster, listDatabases
command returns all databaseslistDatabases
action on the cluster, listDatabases
command errors with insufficient permissions.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.
listDatabases
privilege action on the cluster, the listDatabases
command returns a list of all existing databases.listDatabases
privilege action on the cluster, the listDatabases
command only returns a list of databases for which the user has the find
action.Starting in MongoDB 4.2, if the client that issued the listDatabases
disconnects before the operation completes, MongoDB marks the listDatabases
for termination (i.e. killOp
on the operation).
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 can also be run when the member is in STARTUP2
. However, the operations wait until the member transitions to RECOVERING
.
Run listDatabases
against the admin
database:
The following is an example of a listDatabases
result:
New in version 3.6.版本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:
New in version 3.6.版本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
:
When executed against a mongos
instance, listDatabases
:
shards
embedded document to each database’s summary document if nameOnly: false
, andlocal
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 and indexes.
For example:例如:
See also参阅