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.结果取决于用户的权限。有关详细信息,请参阅所需访问权限。Thedb.getCollectionInfos()helper wraps thelistCollectionscommand.db.getCollectionInfos()辅助程序包装listCollections命令。
Syntax语法
The db.getCollectionInfos() method has the following parameters:db.getCollectionInfos()方法具有以下参数:
filter |
| |
options |
Options选项
You can specify the following options in the 您可以在options document of a db.getCollectionInfos() command:db.getCollectionInfo()命令的options文档中指定以下选项:
nameOnly |
| |
authorizedCollections |
|
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.authorizedCollections和nameOnly都设置为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.authorizedCollections和nameOnly都设置为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 collectionslists the non-system collections for the database.show collections列出了数据库的非系统集合。For users without the required access,对于没有所需访问权限的用户,show collectionslists 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操作要求该成员处于PRIMARY或SECONDARY状态。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.readOnly为true:
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"
}
}
]