对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

MongoDB\Database::listCollections()

MongoDB\Database::listCollections()

返回此数据库中所有collection的信息。

function listCollections(array $options = []): MongoDB\Model\CollectionInfoIterator
$options : array

指定所需选项的数组。

名称
类型
说明

authorizedCollections

布尔

A flag that determines which collections are returned based on the user privileges when access control is enabled. For more information, see the listCollections command documentation.

1.12版本新增。

comment

混合

使用户可以指定任意注释来帮助通过数据库分析器、currentOp 输出和日志来跟踪操作。

自 MongoDB 4.4 起此选项可用,如果为旧服务器版本指定,则会在执行时导致异常。

1.13版本新增。

筛选器

数组|对象

用于过滤集合列表的查询表达式。

您可以为collection字段指定查询表达式(例如 name 、 options )。

maxTimeMS

整型

The cumulative time limit in milliseconds for processing operations on the cursor. MongoDB aborts the operation at the earliest following interrupt point.

会话

与操作相关联的客户端会话。

1.3版本新增。

可遍历的MongoDB\Model\CollectionInfoIterator ,其中包含每个collection在数据库中的MongoDB\Model\CollectionInfo对象。

以下示例列出了test数据库中的所有collection:

<?php
$database = (new MongoDB\Client)->test;
foreach ($database->listCollections() as $collectionInfo) {
var_dump($collectionInfo);
}

而输出将类似如下所示:

object(MongoDB\Model\CollectionInfo)#3 (2) {
["name"]=>
string(11) "restaurants"
["options"]=>
array(0) {
}
}
object(MongoDB\Model\CollectionInfo)#3 (2) {
["name"]=>
string(5) "users"
["options"]=>
array(0) {
}
}
object(MongoDB\Model\CollectionInfo)#3 (2) {
["name"]=>
string(6) "restos"
["options"]=>
array(0) {
}
}

以下示例列出了test数据库中名称以"rest"开头的所有collection:

<?php
$database = (new MongoDB\Client)->test;
$collections = $database->listCollections([
'filter' => [
'name' => new MongoDB\BSON\Regex('^rest.*'),
],
]);
foreach ($collections as $collectionInfo) {
var_dump($collectionInfo);
}

而输出将类似如下所示:

object(MongoDB\Model\CollectionInfo)#3 (2) {
["name"]=>
string(11) "restaurants"
["options"]=>
array(0) {
}
}
object(MongoDB\Model\CollectionInfo)#3 (2) {
["name"]=>
string(6) "restos"
["options"]=>
array(0) {
}
}