listDatabases

On this page本页内容

Definition定义

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命令可以包含以下可选字段:

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:您可以在listDatabases输出中的任何字段上指定条件:

  • name
  • sizeOnDisk
  • empty
  • shards
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, so listDatabases returns the name and size information of each database.默认值为false,因此listDatabases返回每个数据库的名称和大小信息。

authorizedDatabasesboolean

Optional. 可选。A flag that determines which databases are returned based on the user privileges when access control is enabled.启用访问控制时,根据用户权限确定返回哪些数据库的标志。

  • If authorizedDatabases is unspecified, and如果未指定authorizedDatabases,并且

    • If the user has listDatabases action on the cluster resource, listDatabases command returns all databases.如果用户对群集资源执行listDatabases操作,listDatabases命令将返回所有数据库。
    • If the user does not have listDatabases action on the cluster:如果用户在群集上没有listDatabases操作:

      • For MongoDB 4.0.6+, listDatabases command returns only the databases for which the user has privileges (including databases for which the user has privileges on specific collections).对于MongoDB 4.0.6+listDatabases命令仅返回用户拥有权限的数据库(包括用户拥有特定集合权限的数据库)。
      • For MongoDB 4.0.5, listDatabases command returns only the databases for which the user has the find action on the database resource (and not the collection resource).对于MongoDB 4.0.5listDatabases命令仅返回用户对数据库资源(而不是集合资源)执行find操作的数据库。
  • If authorizedDatabases is true,如果authorizedDatabasestrue

    • For MongoDB 4.0.6+, listDatabases command returns only the databases for which the user has privileges (including databases for which the user has privileges on specific collections).对于MongoDB 4.0.6+listDatabases命令仅返回用户拥有权限的数据库(包括用户拥有特定集合权限的数据库)。
    • For MongoDB 4.0.5, listDatabases command returns only the databases for which the user has the find action on the database resource (and not the collection resource).对于MongoDB 4.0.5listDatabases命令仅返回用户对数据库资源(而不是集合资源)执行find操作的数据库。
  • If authorizedDatabases is false, and如果authorizedDatabasesfalse,并且

For more information, see Behavior.有关详细信息,请参阅行为

New in version 4.0.5.在版本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:设置后,此注释将与此命令的记录一起显示在以下位置:

A comment can be any valid BSON type(string, integer, object, array, etc).注释可以是任何有效的BSON类型(字符串、整数、对象、数组等)。

New in version 4.4.在版本4.4中新增

Behavior行为

When authentication is enabled:启用身份验证时:

Note注意

For mongosh connected to earlier versions of MongoDB deployment (e.g. 3.6.10),对于连接到早期版本MongoDB部署(例如3.6.10)的mongosh

  • If the user has listDatabases action on the cluster resource, show dbs returns all databases.如果用户对集群资源执行listDatabases操作,show dbs将返回所有数据库。
  • If the user does not have 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 listDatabases command returns different values based on the privileges assigned to the user who executes the command and the authorizedDatabases command option:对于MongoDB 4.0.6+,listDatabases命令根据分配给执行该命令的用户的权限和authorizedDatabases命令选项返回不同的值:

  • 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

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:对于MongoDB 4.0.5,listDatabases命令根据分配给执行该命令的用户的权限和authorizedDatabases命令选项返回不同的值:

  • If authorizedDatabases is unspecified, and如果未指定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 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

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.

Client Disconnection客户端断开连接

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).从MongoDB 4.2开始,如果在操作完成之前发出listDatabases的客户端断开连接,MongoDB会标记listDatabases终止(即操作上的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. 从MongoDB 4.4开始,要在副本集成员上运行,listDatabases操作要求成员处于PRIMARYSECONDARY状态。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

Examples示例

List Database Names and Sizes列出数据库名称和大小

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
}

List Database Names Only仅列出数据库名称

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
}

List Databases That Match the Filter列出与筛选器匹配的数据库

Run listDatabases against the admin database. admin数据库运行listDatabasesSpecify 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/ } } )

Sharded Clusters分片状群集

When executed against a mongos instance, listDatabases:mongos实例执行时,listDatabases

  • adds a shards embedded document to each database's summary document if nameOnly: false, and如果nameOnly:false,则向每个数据库的摘要文档添加一个嵌入shards的文档,并且
  • excludes the 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
}
Tip提示

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 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字段的总和(以字节为单位)。

listDatabases.totalSizeMb

Type: Integer

Sum of all the sizeOnDisk fields, expressed in megabytes.所有sizeOnDisk字段的总和,以兆字节表示。

listDatabases.ok

Type: Integer

Return value for the command. 命令的返回值。A value of 1 indicates success.1表示成功。

←  listCollectionslistIndexes →