On this page本页内容
listDatabases
The listDatabases
command provides a list of all existing databases along with basic statistics about them. listDatabases
命令提供所有现有数据库的列表以及有关它们的基本统计信息。The listDatabases
must run against the admin
database, as in the following example:listDatabases
必须针对管理数据库运行,如下例所示:
db.adminCommand( { listDatabases: 1 } )
The value (e.g. 值(例如1
) does not affect the output of the command.1
)不会影响命令的输出。
The listDatabases
command can take the following optional fields:listDatabases
命令可以包含以下可选字段:
filter | document |
|
nameOnly | boolean |
|
authorizedDatabases | boolean |
|
comment | any |
|
When authentication is enabled:启用身份验证时:
For 对于连接到早期版本MongoDB部署(例如3.6.10)的mongosh
connected to earlier versions of MongoDB deployment (e.g. 3.6.10),mongosh
,
listDatabases
action on the cluster resource, show dbs
returns all databases.listDatabases
操作,show dbs
将返回所有数据库。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).listDatabases
操作,show dbs
只返回用户有权限的数据库(包括用户对特定集合有权限的那些数据库)。For MongoDB 4.0.6+, the 对于MongoDB 4.0.6+,listDatabases命令根据分配给执行该命令的用户的权限和authorizedDatabases命令选项返回不同的值:listDatabases
command returns different values based on the privileges assigned to the user who executes the command and the authorizedDatabases
command option:
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, listDatabases
command returns only the databases for which the user has privileges (including databases for which the user has privileges on specific collections).
If 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).
If authorizedDatabases
is false
, 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.
For MongoDB 4.0.5, the 对于MongoDB 4.0.5,listDatabases
command returns different values based on the privileges assigned to the user who executes the command and the authorizedDatabases
command option:listDatabases
命令根据分配给执行该命令的用户的权限和authorizedDatabases
命令选项返回不同的值:
If 如果未指定authorizedDatabases
is unspecified, andauthorizedDatabases
,并且
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, listDatabases
command returns only the databases for which the user has the find
action on the database resource (and not the collection resource).
If 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).
If authorizedDatabases
is false
, 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.
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 listDatabases
privilege action on the cluster, the listDatabases
command returns a list of all existing databases.
If the user does not have the 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 从MongoDB 4.2开始,如果在操作完成之前发出listDatabases
disconnects before the operation completes, MongoDB marks the listDatabases
for termination (i.e. killOp
on the operation).listDatabases
的客户端断开连接,MongoDB会标记listDatabases
终止(即操作上的killOp
)。
Starting in MongoDB 4.4, to run on a replica set member, 从MongoDB 4.4开始,要在副本集成员上运行,listDatabases
operations require the member to be in PRIMARY
or SECONDARY
state. listDatabases
操作要求成员处于PRIMARY
或SECONDARY
状态。If the member is in another state, such as 如果成员处于其他状态,如STARTUP2
, the operation errors.STARTUP2
,则操作将出错。
In previous versions, the operations can also be run when the member is in 在以前的版本中,当成员处于STARTUP2
. STARTUP2
时,也可以运行这些操作。However, the operations wait until the member transitions to 但是,这些操作会等到成员转换到RECOVERING
.RECOVERING
。
Run 对listDatabases
against the admin
database:admin
数据库运行listDatabases
:
db.adminCommand( { listDatabases: 1 } )
The following is an example of a 以下是listDatabases
result:listDatabases
结果的示例:
{ "databases" : [ { "name" : "admin", "sizeOnDisk" : 83886080, "empty" : false }, { "name" : "local", "sizeOnDisk" : 83886080, "empty" : false }, { "name" : "test", "sizeOnDisk" : 83886080, "empty" : false } ], "totalSize" : 251658240, "totalSizeMb" : 251, "ok" : 1 }
Run 对listDatabases
against the admin
database. Specify the nameOnly: true
option:admin
数据库运行listDatabases
。指定nameOnly:true
选项:
db.adminCommand( { listDatabases: 1, nameOnly: true} )
The following is an example of a 以下是使用listDatabases
results when run with the nameOnly: true
option:nameOnly:true
选项运行时的listDatabases
结果示例:
{ "databases" : [ { "name" : "admin" }, { "name" : "local" }, { "name" : "test" } ], "ok" : 1 }
Run 对listDatabases
against the admin
database. admin
数据库运行listDatabases
。Specify the 指定filter
option to only list databases that match the specified filter criteria.filter
选项以仅列出符合指定筛选条件的数据库。
For example, the following specifies a filter such that 例如,下面指定了一个筛选器,以便listDatabases
only returns information on databases whose name matches the specified regular expression
:listDatabases
仅返回名称与指定regular expression
匹配的数据库的信息:
db.adminCommand( { listDatabases: 1, filter: { "name": /^rep/ } } )
When executed against a 对mongos
instance, listDatabases
:mongos
实例执行时,listDatabases
:
shards
embedded document to each database's summary document if nameOnly: false
, andnameOnly:false
,则向每个数据库的摘要文档添加一个嵌入shards
的文档,并且local
database.local
数据库。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.shards
嵌入文档中的每个元素都由一个字段组成,该字段的键给出该分片上集合的名称,其值表示集合的大小(以字节为单位)。
The sizeOnDisk
field represents the total size of all listed collections and indexes.sizeOnDisk
字段表示所有列出的集合和索引的总大小。
For example:例如:
{ "databases" : [ { "name" : "admin", "sizeOnDisk" : 16384, "empty" : false, "shards" : { "config" : 16384 } }, { "name" : "config", "sizeOnDisk" : 176128, "empty" : false, "shards" : { "clients" : 28672, "patients" : 8192, "config" : 139264 } }, { "name" : "test", "sizeOnDisk" : 12288, "empty" : false, "shards" : { "clients" : 12288 } } ], "totalSize" : 204800, "totalSizeMb" : 0, "ok" : 1 }
listDatabases.databases
Type: Array
Array of documents, each containing information on a single database.文档数组,每个文档包含单个数据库的信息。
listDatabases.databases.sizeOnDisk
Type: Integer
Total size of the database files on disk, expressed in bytes.磁盘上数据库文件的总大小,以字节表示。
listDatabases.databases.shards
Type: Document
Each element in the shards
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.shards
文档中的每个元素都包含一个字段,该字段的键给出了该shard上的集合的名称,其值表示集合的大小(以字节为单位)。
只有当shards
only appears in the output if nameOnly: false
.nameOnly:false
时,shards
才会出现在输出中。
See Sharded Clusters for details.有关详细信息,请参阅分片集群。
listDatabases.totalSize
Type: Integer
Sum of all the 所有sizeOnDisk
fields in bytes.sizeOnDisk
字段的总和(以字节为单位)。