定义
参数
$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.
会话
与操作相关联的客户端会话。
1.3版本新增。
Return Values
可遍历的MongoDB\Model\CollectionInfoIterator ,其中包含每个collection在数据库中的MongoDB\Model\CollectionInfo对象。
例子
以下示例列出了test数据库中的所有collection:
$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:
$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) { } }
另请参阅
listCollections command reference in the MongoDB manual
枚举集合规范