Database Manual / Reference / mongosh Methods / Databases

db.getCollectionInfos() (mongosh method方法)

Definition定义

db.getCollectionInfos(filter, options)

Returns an array of documents that contain collection or view information, such as name and options, for the current database. 返回包含当前数据库的集合或视图信息(如名称和选项)的文档数组。The results depend on the user's privilege. For details, see Required Access.结果取决于用户的权限。有关详细信息,请参阅所需访问权限

The db.getCollectionInfos() helper wraps the listCollections command.db.getCollectionInfos()辅助程序包装listCollections命令。

Syntax语法

The db.getCollectionInfos() method has the following parameters:db.getCollectionInfos()方法具有以下参数:

Parameter参数Type类型Description描述
filterdocument文档

Optional. 可选。A query predicate to filter the list of collections.用于筛选集合列表的查询谓词。

You can specify a query predicate on any of the fields returned by db.getCollectionInfos().您可以在db.getCollectionInfos()返回的任何字段上指定查询谓词。

optionsdocument文档Optional. 可选。A document that specifies additional options for the command. For a list of available options, see Options.为命令指定其他选项的文档。有关可用选项的列表,请参阅选项

Options选项

You can specify the following options in the options document of a db.getCollectionInfos() command:您可以在db.getCollectionInfo()命令的options文档中指定以下选项:

Option选项Type类型Description描述
nameOnlyboolean布尔值

Optional. 可选。A flag to indicate whether the command returns just the name and type (view, collection, or timeseries) or returns both the name and other information.一个标志,用于指示命令是只返回名称和类型(viewcollectiontimeseries),还是同时返回名称和其他信息。

The default value is false.默认值为false

When nameOnly is true, your filter expression can only filter based on a collection's name and type. No other fields are available.nameOnlytrue时,filter表达式只能根据集合的名称和类型进行筛选。没有其他字段可用。

For an example, see Return Only Collection Names.例如,请参阅仅返回集合名称

authorizedCollectionsboolean布尔值

Optional. 可选。When set to true and used with nameOnly: true, allows a user without the required privilege (such as the listCollections action on the database) to run db.getCollectionInfos() when access control is enforced.当设置为true并与nameOnly:true一起使用时,允许没有所需权限(如数据库上的listCollections操作)的用户在实施访问控制时运行db.getCollectionInfo()

When both authorizedCollections and nameOnly options are set to true, the command returns only those collections for which the user has privileges. For example:authorizedCollectionnameOnly选项都设置为true时,该命令只返回用户有权限的集合。例如:

  • If a user has the find action on specific collections, the command returns only those collections.如果用户对特定集合执行find操作,则该命令仅返回这些集合。
  • If a user has any permission action on the database resource, the command lists all collections in the database.如果用户对数据库资源有任何权限操作,则该命令将列出数据库中的所有集合。

The default value is false, meaning the user must have listCollections action on the database to run the command.默认值为false,这意味着用户必须对数据库执行listCollections操作才能运行该命令。

For a user who has listCollections action on the database, this option has no effect since the user has privileges to list the collections in the database.对于在数据库上具有listCollections操作的用户,此选项无效,因为该用户具有在数据库中列出集合的权限。

When used without nameOnly: true, this option has no effect.当不使用nameOnly: true时,此选项无效。

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Required Access所需访问权限

The listCollections command and its wrapper db.getCollectionInfos() require the listCollections action when access control is enforced. Users must have privileges that grant the listCollections action on the database to run listCollections.当执行访问控制时,listCollections命令及其包装器db.getCollectionInfos()需要listCollections操作。用户必须具有在数据库上授予listCollections操作的权限才能运行listCollections

For example, the following command grants the privilege to run db.getCollectionInfos() against the test database:例如,以下命令授予对test数据库运行db.getCollectionInfos()的权限:

{ resource: { db: "test", collection: "" }, actions: [ "listCollections" ] }

The built-in role read provides the privilege to run listCollections for a specific database.内置的角色read提供了为特定数据库运行listCollections的权限。

Users without the required read privilege can run listCollections when authorizedCollections and nameOnly are both set to true. In this case, the command returns the names and types for collection(s) where the user has privileges.authorizedCollectionsnameOnly都设置为true时,没有所需读取权限的用户可以运行listCollections。在这种情况下,该命令返回用户具有权限的集合的名称和类型。

For example, consider a user with a role that grants the following find privilege:例如,考虑一个具有授予以下find权限的角色的用户:

{ resource: { db: "sales", collection: "currentQuarter" }, actions: [ "find" ] }

The user can run listCollections if authorizedCollections and nameOnly are both set to true.如果authorizedCollectionsnameOnly都设置为true,则用户可以运行listCollections

db.runCommand(
{
listCollections: 1.0,
authorizedCollections: true,
nameOnly: true
}
)

The operation returns the name and type of the currentQuarter collection.该操作返回currentQuarter集合的名称和类型。

However, the following operations return an error if the user does not have the required access authorization:但是,如果用户没有所需的访问权限,以下操作将返回错误:

db.runCommand(
{
listCollections: 1.0,
authorizedCollections: true
}
)

db.runCommand(
{
listCollections: 1.0,
nameOnly: true
}
)

show collections

The mongosh method show collections is similar to:mongosh方法show collections类似于:

db.runCommand(
{
listCollections: 1.0,
authorizedCollections: true,
nameOnly: true
}
)
  • For users with the required access, show collections lists the non-system collections for the database.对于具有所需访问权限的用户,show collections列出了数据库的非系统集合。
  • For users without the required access, show collections lists only the collections for which the users has privileges.对于没有所需访问权限的用户,show collections仅列出用户具有权限的集合。

Behavior行为

Client Disconnection客户端断开连接

If the client that issued db.getCollectionInfos() disconnects before the operation completes, MongoDB marks db.getCollectionInfos() for termination using killOp.如果发出db.getCollectionInfos()的客户端在操作完成之前断开连接,MongoDB将使用killOp标记db.getCollectionInfos()以终止。

Replica Set Member State Restriction副本集成员状态限制

To run on a replica set member, listCollections operations require the member to be in PRIMARY or SECONDARY state. 要在副本集成员上运行,listCollections操作要求该成员处于PRIMARYSECONDARY状态。If the member is in another state, such as STARTUP2, the operation errors.如果成员处于另一种状态,如STARTUP2,则操作错误。

Examples示例

Return Information for All Collections in a Database返回数据库中所有集合的信息

The following returns information for all collections in the example database:以下内容返回example数据库中所有集合的信息:

use example
db.getCollectionInfos()
   [
{
"name" : "employees",
"type" : "collection",
"options" : {
"flags" : 1,
"validator" : {
"$or" : [
{
"phone" : {
"$exists" : true
}
},
{
"email" : {
"$exists" : true
}
}
]
}
},
"info" : {
"readOnly" : false,
"uuid" : UUID("222e18ca-4a10-4a42-a8fe-c39255cc4c55")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "example.employees"
}
},
{
"name" : "products",
"type" : "collection",
"options" : {
"flags" : 1
},
"info" : {
"readOnly" : false,
"uuid" : UUID("1bc898b2-3b91-45e4-9d8b-0be462d5a157")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "example.products"
}
},
{
"name" : "mylogs",
"type" : "collection",
"options" : {
"capped" : true,
"size" : 256
},
"info" : {
"readOnly" : true,
"uuid" : UUID("8e62116d-b6a0-490a-808c-258ccb7ea947")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "example.mylogs"
}
}
]

Return Only Collection Names仅返回集合名称

To return only the names of collections in the current database, specify the nameOnly: true option. For example:要仅返回当前数据库中集合的名称,请指定nameOnly: true选项。例如:

use example
db.getCollectionInfos( {}, { nameOnly: true } )
[
{ name: 'products', type: 'collection' },
{ name: 'weather', type: 'timeseries' },
{ name: 'system.buckets.weather', type: 'collection' },
{ name: 'system.views', type: 'collection' },
{ name: 'sales', type: 'collection' }
]

Return Information for a Specific Collection返回特定集合的信息

To request collection information for a specific collection, specify the collection name in the filter document. The following example returns an array with a single document that details the collection information for the employees collection in the example database.要请求特定集合的集合信息,请在筛选器文档中指定集合名称。以下示例返回一个包含单个文档的数组,该文档详细说明了example数据库中employees集合的集合信息。

use example
db.getCollectionInfos( { name: "employees" } )
[
{
"name" : "employees",
"type" : "collection",
"options" : {
"flags" : 1,
"validator" : {
"$or" : [
{
"phone" : {
"$exists" : true
}
},
{
"email" : {
"$exists" : true
}
}
]
}
},
"info" : {
"readOnly" : false,
"uuid" : UUID("222e18ca-4a10-4a42-a8fe-c39255cc4c55")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "example.employees"
}
}
]

Return Information for Read-Only Collections只读集合的返回信息

You can specify a filter on any of the fields returned by db.getCollectionInfos().您可以在db.getCollectionInfo()返回的任何字段上指定筛选器。

For example, the following command returns information for all collections in the example database where info.readOnly is true:例如,以下命令返回example数据库中所有集合的信息,其中info.readOnlytrue

use example
db.getCollectionInfos( { "info.readOnly" : true } )
[
{
"name" : "mylogs",
"type" : "collection",
"options" : {
"capped" : true,
"size" : 256
},
"info" : {
"readOnly" : true,
"uuid" : UUID("8e62116d-b6a0-490a-808c-258ccb7ea947")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "example.mylogs"
}
}
]