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

MongoDB\Collection::listIndexes()

MongoDB\Collection::listIndexes()

返回此collection的所有索引的信息。

function listIndexes(array $options = []): Iterator
$options : array

指定所需选项的数组。

名称
类型
说明

comment

混合

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

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

1.13版本新增。

maxTimeMS

整型

处理游标操作的累计时间限制(以毫秒为单位)。MongoDB 最早会在中断点之后中止操作。

会话

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

1.3版本新增。

一个迭代器实例,为集合的每个索引提供一个MongoDB\Model\IndexInfo对象。

MongoDB\Exception\InvalidArgumentException 用于与参数或选项解析相关的错误。

MongoDB\ 驱动程序\Exception\RuntimeException 用于扩展级别的其他错误(例如连接错误)。

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

<?php $collection = (new MongoDB\Client)->test->restaurants; foreach ($collection->listIndexes() as $index) { var_dump($index);}

而输出将类似如下所示:

object(MongoDB\Model\IndexInfo)#8 (4) { ["v"]=> int(1) ["key"]=> array(1) { ["_id"]=> int(1) } ["name"]=> string(4) "_id_" ["ns"]=> string(16) "test.restaurants"}object(MongoDB\Model\IndexInfo)#12 (4) { ["v"]=> int(1) ["key"]=> array(1) { ["cuisine"]=> float(-1) } ["name"]=> string(10) "cuisine_-1" ["ns"]=> string(16) "test.restaurants"}object(MongoDB\Model\IndexInfo)#8 (4) { ["v"]=> int(1) ["key"]=> array(1) { ["borough"]=> float(1) } ["name"]=> string(9) "borough_1" ["ns"]=> string(16) "test.restaurants"}