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

MongoDB\Database::listCollectionNames()

1.7版本新增。

MongoDB\Database::listCollectionNames()

返回此数据库中所有collection的名称。

function listCollectionNames(array $options = []): Iterator
$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.

会话

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

迭代器,提供数据库中每个集合的名称。

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

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

而输出将类似如下所示:

string(11) "restaurants"
string(5) "users"
string(6) "restos"

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

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

而输出将类似如下所示:

string(11) "restaurants"
string(6) "restos"

注意

枚举集合名称时,筛选表达式只能根据集合的名称和类型进行筛选。 没有其他可用字段。