对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

listDatabases(数据库命令)

listDatabases

listDatabases 命令返回所有现有数据库的未排序列表以及每个数据库的基本统计信息。您必须对 admin数据库运行listDatabases 命令。

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

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

注意

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

db.adminCommand(
{
listDatabases: 1
}
)

该值(示例 1)不影响命令的输出。

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

字段
类型
说明

filter

文档

可选。确定列出哪些数据库的查询谓词。

您可以在 listDatabases 输出中的任何字段上指定条件:

  • name

  • sizeOnDisk

  • empty

  • shards

nameOnly

布尔

可选.指示命令是只返回数据库名称,还是返回数据库名称和大小信息。

默认为 false,因此 listDatabases 会返回每个数据库的名称和大小信息。

authorizedDatabases

布尔

可选。该标志用于确定启用访问控制时根据用户特权返回哪些数据库。

  • 如果未指定 authorizedDatabases,并且

    • 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).

  • 如果 authorizedDatabasesfalse,并且

如需了解更多信息,请参阅行为

comment

any

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

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

启用身份验证后,listDatabases会根据用户的权限和authorizedDatabases选项返回不同的结果:

  • 如果未指定 authorizedDatabases,并且

    • 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).

  • 如果 authorizedDatabasesfalse,并且

If the client that issued listDatabases disconnects before the operation completes, MongoDB marks listDatabases for termination using killOp.

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.

针对 admin数据库运行 listDatabases

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
}

针对 admin数据库运行 listDatabases。指定 nameOnly: true 选项:

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

示例输出:

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

针对 admin 数据库运行 listDatabases。指定 filter 选项以仅列出与过滤器条件匹配的数据库。

例如,以下过滤器仅返回名称与指定的 regular expression 匹配的数据库:

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

当针对 mongos 实例运行时,listDatabases

  • 如果 nameOnly: false,则将 shards嵌入式文档添加到每个数据库的摘要文档中。

  • 排除 local 数据库。

shards 嵌入式文档中的每个字段都使用分片名称作为密钥,并使用数据库的大小(以字节为单位)作为值。

sizeOnDisk 字段表示列出数据库中所有集合和索引的总大小。

例如:

{
"databases" : [
{
"name" : "admin",
"sizeOnDisk" : 16384,
"empty" : false,
"shards" : {
"config" : 16384
}
},
{
"name" : "config",
"sizeOnDisk" : 176128,
"empty" : false,
"shards" : {
"shard01" : 28672,
"shard02" : 8192,
"config" : 139264
}
},
{
"name" : "test",
"sizeOnDisk" : 12288,
"empty" : false,
"shards" : {
"shard01" : 12288
}
}
],
"totalSize" : 204800,
"totalSizeMb" : 0,
"ok" : 1
}
listDatabases.databases

类型:数组

文档数组,其中每个文档都包含单个数据库的信息。

listDatabases.databases.name

类型:字符串

数据库名称。

listDatabases.databases.sizeOnDisk

类型:整数

磁盘上数据库文件的总大小(以字节表示)。

listDatabases.databases.empty

类型:布尔值

指定数据库是否为空。

listDatabases.databases.shards

类型:文档

shards 文档中的每个字段都使用分片名称作为关键,并使用数据库的大小(以字节为单位)作为值。

shards 仅当 nameOnly: false 时才出现在输出中。

请参阅分片集群,了解详情。

listDatabases.totalSize

类型:整数

所有 sizeOnDisk 字段的总和(以字节为单位)。

listDatabases.totalSizeMb

类型:整数

所有 sizeOnDisk 字段的总和,以兆字节表示。

listDatabases.ok

类型:整数

命令的返回值。值为 1 表示成功。