listCollections
On this page本页内容
Definition定义
listCollections
-
Retrieves information, including the names and creation options, for the collections and views in a database.检索数据库中集合和视图的信息,包括名称和创建选项。ThelistCollections
command returns a document that contains information you can use to create a cursor on the collection.listCollections
命令返回一个文档,该文档包含可用于在集合上创建游标的信息。mongosh
provides thedb.getCollectionInfos()
and thedb.getCollectionNames()
helper methods.mongosh
提供了db.getCollectionInfos()
和db.getCollectionNames()
辅助方法。
Syntax语法
The command has the following syntax:该命令具有以下语法:
db.runCommand(
{
listCollections: 1,
filter: <document>,
nameOnly: <boolean>,
authorizedCollections: <boolean>,
comment: <any>
}
)
Command Fields命令字段
The command can take the following optional fields:该命令可以采用以下可选字段:
filter | document | listCollections . listCollections 返回的任何字段上指定查询表达式。 |
nameOnly | boolean | view or collection ) does not take collection-level locks whereas returning full collection information locks each collection in the database.view 或collection )不会获得集合级别的锁定,而返回完整的集合信息会锁定数据库中的每个集合。false . false 。Note nameOnly is true , your filter expression can only filter based on a collection's name and type.nameOnly 为true 时,filter 表达式只能根据集合的名称和类型进行筛选。 |
authorizedCollections | boolean | true and used with nameOnly: true , that allows a user without the required privilege (i.e. listCollections action on the database) to run the command when access control is enforced.true 并与nameOnly:true 一起使用时,它允许没有所需权限(即数据库上的listCollections 操作)的用户在强制执行访问控制时运行该命令。authorizedCollections and nameOnly options are set to true, the command returns only those collections for which the user has privileges. authorizedCollections 和nameOnly 选项都设置为true 时,该命令仅返回用户具有权限的集合。find action on specific collections, the command returns only those collections; or, if a user has find or any other action, on the database resource, the command lists all collections in the database.find 操作,则该命令仅返回这些集合;或者,如果用户对数据库资源具有find 或任何其他操作,则该命令会列出数据库中的所有集合。false . false 。listCollections action on the database to run the command.listCollections 操作才能运行该命令。listCollections action on the database, this option has no effect since the user has privileges to list the collections in the database.listCollections 操作的用户,此选项无效,因为该用户具有在数据库中列出集合的权限。nameOnly: true , this option has no effect. That is, the user must have the required privileges to run the command when access control is enforced. nameOnly:true 的情况下使用时,此选项无效。也就是说,在实施访问控制时,用户必须具有运行命令所需的权限。 |
comment | any |
|
Behavior行为
Filter筛选器
Use a filter to limit the results of 使用筛选器限制listCollections
. listCollections
的结果。You can specify a 您可以对filter
on any of the fields returned in the listCollections
result set.listCollections
结果集中返回的任何字段指定筛选器。
Locks锁定
listCollections
lock behavior:锁定行为:
Earlier than MongoDB 5.0,在MongoDB 5.0之前,当listCollections
takes an intent shared lock lock on each collection in the database whenlistCollections
holds an intent shared lock on the database.listCollections
对数据库持有意向共享锁时,listCollections
会对数据库中的每个集合持有意向共享锁定。Starting in MongoDB 5.0,从MongoDB 5.0开始,listCollections
doesn't take an intent shared lock on a collection or database.listCollections
不会对集合或数据库使用意向共享锁。listCollections
isn't blocked by operations holding an exclusive write lock on a collection.未被对集合持有独占写锁的操作阻止。
To learn about locks, see FAQ: Concurrency.要了解有关锁的信息,请参阅常见问题解答:并发。
Client Disconnection客户端断开连接
Starting in MongoDB 4.2, if the client that issued 从MongoDB 4.2开始,如果在操作完成之前发出listCollections
disconnects before the operation completes, MongoDB marks listCollections
for termination using killOp
.listCollections
的客户端断开连接,MongoDB会使用killOp
标记listCollections
以终止。
Replica Set Member State Restriction副本集成员状态限制
Starting in MongoDB 4.4, to run on a replica set member, 从MongoDB 4.4开始,要在副本集成员上运行,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
,则操作将出错。
In previous versions, the operations also run when the member is in 在以前的版本中,成员处于STARTUP2
. STARTUP2
时也会运行这些操作。The operations wait until the member transitioned to 操作将等待,直到成员转换到RECOVERING
.RECOVERING
。
Required Access所需访问权限
The 在实施访问控制时,listCollections
command requires the listCollections
action when access control is enforced. listCollections
命令需要listCollections
操作。Users must have privileges that grant the 用户必须具有对数据库授予listCollections
action on the database to run 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
. authorizedCollections
和nameOnly
都设置为true
时,没有所需read
权限的用户可以运行listCollections
。In this case, the command returns the names and types for collection(s) where the user has privileges.在这种情况下,该命令返回用户具有权限的集合的名称和类型。
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 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
仅列出用户具有权限的集合。
Output输出
listCollections.cursor
-
A document that contains information with which to create a cursor to documents that contain collection names and options.包含信息的文档,用于创建指向包含集合名称和选项的文档的游标。The cursor information includes the cursor id, the full namespace for the command, as well as the first batch of results. Each document in the batch output contains the following fields:游标信息包括游标id、命令的完整名称空间以及第一批结果。批处理输出中的每个文档都包含以下字段:Field字段Type类型Description描述name String Name of the collection.集合的名称。type String Type of data store.数据存储的类型。Returns返回集合的collection
for collections,view
for views, andtimeseries
for time series collection.collection
、视图的view
和时间序列集合的timeseries
。options Document Collection options.集合选项。
These options correspond directly to the options available in这些选项直接对应于db.createCollection()
.db.createCollection()
中可用的选项。For the descriptions on the options, see有关选项的描述,请参阅db.createCollection()
.db.createCollection()
。info Document Lists the following fields related to the collection:列出与集合相关的以下字段:- readOnly
boolean
.If如果为true
the data store is read only.true
,则数据存储为只读。- uuid
- UUID.
Once established, the collection UUID does not change.一旦建立,集合UUID就不会更改。The collection UUID remains the same across replica set members and shards in a sharded cluster.在分片集群中,副本集成员和分片之间的集合UUID保持不变。
idIndex Document Provides information on the提供有关集合的_id
index for the collection._id
索引的信息。
Example实例
List All Collections列出所有集合
The music
database contains three collections, motorhead
, taylorSwift
, and ramones
.music
数据库包含三个集合,motohead
、taylorSwift
和ramones
。
To list the collections in the database, you can use the built-in 要列出数据库中的集合,可以使用内置的mongosh
command, show collections.mongosh
命令show collections
。
show collections
The output is:输出为:
motorhead
ramones
taylorSwift
To get a similar list with the 要使用listCollections
collections command, use the nameOnly
option.listCollections
集合命令获取类似的列表,请使用nameOnly
选项。
db.runCommand(
{
listCollections: 1.0,
nameOnly: true
}
)
The output is:输出为:
{
cursor: {
id: Long("0"),
ns: 'music.$cmd.listCollections',
firstBatch: [
{ name: 'motorhead', type: 'collection' },
{ name: 'taylorSwift', type: 'collection' },
{ name: 'ramones', type: 'collection' }
]
},
ok: 1
}
To get more detailed information, remove the 若要获取更多详细信息,请删除nameOnly
option.nameOnly
选项。
db.runCommand(
{
listCollections: 1.0
}
)
The output is:输出为:
{
cursor: {
id: Long("0"),
ns: 'music.$cmd.listCollections',
firstBatch: [
{
name: 'motorhead',
type: 'collection',
options: {},
info: {
readOnly: false,
uuid: new UUID("09ef1858-2831-47d2-a3a7-9a29a9cfeb94")
},
idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }
},
{
name: 'taylorSwift',
type: 'collection',
options: {},
info: {
readOnly: false,
uuid: new UUID("6c46c8b9-4999-4213-bcef-9a36b0cff228")
},
idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }
},
{
name: 'ramones',
type: 'collection',
options: {},
info: {
readOnly: false,
uuid: new UUID("7e1925ba-f2f9-4e42-90e4-8cafd434a6c4")
},
idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }
}
]
},
ok: 1
}
Learn More了解更多信息
For collection options:对于集合选项:
For collection information:集合信息: