1.7版本新增。
定义
参数
$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
混合
筛选器
数组|对象
用于过滤集合列表的查询表达式。
您可以为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.
会话
与操作相关联的客户端会话。
Return Values
迭代器,提供数据库中每个集合的名称。
例子
以下示例列出了test数据库中的所有collection:
$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:
$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"
注意
枚举集合名称时,筛选表达式只能根据集合的名称和类型进行筛选。 没有其他可用字段。
另请参阅
listCollections command reference in the MongoDB manual
枚举集合规范