Docs 菜单

listDatabases

listDatabases

The listDatabases command returns an unsorted list of all existing databases and basic statistics about each database. You must run the listDatabases command against the admin database.

此命令可用于以下环境中托管的部署:

注意

所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

db.adminCommand(
{
listDatabases: 1
}
)

The value (e.g. 1) does not affect the output of the command.

该命令可以采用以下可选字段:

字段
类型
说明

filter

文档

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

The filter option isn't supported on Atlas Free and Shared tier clusters and Serverless Instances.

nameOnly

布尔

Optional. A flag to indicate whether the command should return just the database names, or return both database names and size information.

The default value is false, so listDatabases returns the name and size information of each database.

authorizedDatabases

布尔

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, listDatabases 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 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

For more information, see 行为.

comment

any

可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:

注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。

When 身份验证 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:

  • 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

从 MongoDB 4.2 开始,如果在操作完成之前,发出 listDatabases 的客户端断开连接,MongoDB 将使用killOplistDatabases 标记为终止。

若要在副本集节点上运行,listDatabases 操作要求该节点处于 PRIMARYSECONDARY 状态。如果该节点处于其他状态,如 STARTUP2,则操作错误。

Run listDatabases against the admin database:

db.adminCommand( { listDatabases: 1 } )

示例输出:

{
"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:

db.adminCommand( { listDatabases: 1, nameOnly: true} )

示例输出:

{
"databases" : [
{
"name" : "admin"
},
{
"name" : "local"
},
{
"name" : "test"
}
],
"ok" : 1
}

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:

db.adminCommand( { listDatabases: 1, filter: { "name": /^rep/ } } )

When executed against a mongos instance, listDatabases:

  • adds a shards embedded document to each database's summary document if nameOnly: 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 and indexes.

例如:

{
"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.name

Type: String

数据库名称。

listDatabases.databases.sizeOnDisk

类型:整数

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 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 only appears in the output if nameOnly: false.

See 分片集群 for details.

listDatabases.totalSize

类型:整数

Sum of all the sizeOnDisk fields in bytes.

listDatabases.totalSizeMb

类型:整数

Sum of all the sizeOnDisk fields, expressed in megabytes.

listDatabases.ok

类型:整数

Return value for the command. A value of 1 indicates success.