listCollections
定义
listCollections
Retrieves information, including the names and creation options, for the collections and views in a database.
The
listCollections
command returns a document that contains an unsorted list of all collections and views in the database. You can use the returned document to create a cursor on the collection.mongosh
provides thedb.getCollectionInfos()
and thedb.getCollectionNames()
helper methods.
兼容性
此命令可用于以下环境中托管的部署:
MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
注意
所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
该命令具有以下语法:
db.runCommand( { listCollections: 1, filter: <document>, nameOnly: <boolean>, authorizedCollections: <boolean>, comment: <any> } )
命令字段
该命令可以采用以下可选字段:
字段 | 类型 | 说明 |
---|---|---|
| 文档 | 可选。用于过滤集合列表的查询谓词。 |
| 布尔 | 可选。 指示命令是应仅返回名称和类型( 默认值为 当 |
| 布尔 | 可选。一个标记,当设置为 当 默认值为 对于有权对数据库执行 不与 |
| any | 可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:
注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。 在 |
行为
Filter
Use a filter to limit the results of listCollections
. You can specify a
filter
on any of the fields returned in the
listCollections
result set.
Locks
listCollections
lock behavior:
Earlier than 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.Starting in MongoDB 5.0,
listCollections
doesn't take an intent shared lock on a collection or database.listCollections
isn't blocked by operations holding an exclusive write lock on a collection.
To learn about locks, see FAQ: Concurrency.
客户端断开连接
从 MongoDB 4.2 开始,如果在操作完成之前,发出 listCollections
的客户端断开连接,MongoDB 将使用killOp
将 listCollections
标记为终止。
副本集节点状态限制
若要在副本集节点上运行,listCollections
操作要求该节点处于 PRIMARY
或 SECONDARY
状态。如果该节点处于其他状态,如 STARTUP2
,则操作错误。
必需的访问权限
The listCollections
command requires the
listCollections
action when access control is enforced.
Users must have privileges that grant the listCollections
action
on the database to run listCollections
.
For example, the following command grants the privilege to run
db.getCollectionInfos()
against the test
database:
{ resource: { db: "test", collection: "" }, actions: [ "listCollections" ] }
内置角色 read
提供为特定数据库运行 listCollections
的特权。
当 authorizedCollections
和 nameOnly
都设置为 true
时,没有所需的 read
特权的用户可以运行 listCollections
。在这种情况下,该命令将返回用户对其拥有特权的集合的名称和类型。
例如,假设某个用户具有授予以下 find
特权的角色:
{ resource: { db: "sales", collection: "currentQuarter" }, actions: [ "find" ] }
如果 authorizedCollections
和 nameOnly
都设置为 true
,用户就可以运行 listCollections
。
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
该操作将返回 currentQuarter
集合的名称和类型。
但是,如果用户没有所需访问授权,以下操作会返回错误:
db.runCommand( { listCollections: 1.0, authorizedCollections: true } ) db.runCommand( { listCollections: 1.0, nameOnly: true } )
show collections
mongosh
方法 show collections
类似于:
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
对于具有所需访问权限的用户,
show collections
将列出数据库的非系统集合。对于不具有所需访问权限的用户,
show collections
仅列出用户对其拥有特权的集合。
输出
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:
字段类型说明name
字符串
集合的名称。
type
字符串
Type of data store. Returns
collection
for collections,view
for views, andtimeseries
for 时间序列集合.选项
文档
Collection options.
These options correspond directly to the options available in
db.createCollection()
. For the descriptions on the options, seedb.createCollection()
.info
文档
Lists the following fields related to the collection:
- readOnly
boolean
. Iftrue
the data store is read only.- uuid
- UUID. Once established, the collection UUID does not change. The collection UUID remains the same across replica set members and shards in a sharded cluster.
idIndex
文档
Provides information on the
_id
index for the collection.
例子
List All Collections
The music
database contains three collections, motorhead
,
taylorSwift
, and ramones
.
To list the collections in the database, you can use the built-in
mongosh
command, show collections.
show collections
The output is:
motorhead ramones taylorSwift
To get a similar list with the listCollections
collections command,
use the nameOnly
option.
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.
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 }
了解详情
For collection options:
For collection information: