文档菜单
文档首页
/ / /
PHP库手册
/ /

MongoDB\Collection::listIndexes()

本页内容

  • 定义
  • 参数
  • 返回值
  • 错误/异常
  • 示例
  • 另请参阅
MongoDB\Collection::listIndexes()

返回此集合所有索引的信息。

function listIndexes(array $options = []): MongoDB\Model\IndexInfoIterator
$options : array

一个数组,指定所需的选项。

名称
类型
描述
注释
mixed

允许用户指定任意注释以帮助通过数据库分析器、当前操作输出和日志跟踪操作。数据库分析器当前操作,和日志。

此选项从 MongoDB 4.4 版本开始可用,如果为旧版本服务器指定,将在执行时引发异常。

版本1.13.

maxTimeMS
整数

处理游标操作累积时间限制(毫秒)。MongoDB在最早的中断点处终止操作。

会话

用于关联操作的客户端会话。

版本1.3.

可遍历的MongoDB\Model\IndexInfoIterator,其中包含每个索引的MongoDB\Model\IndexInfo对象。

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

MongoDB\Driver\Exception\RuntimeException,用于扩展级别的其他错误(例如连接错误)。

以下示例列出了测试数据库中 restaurants 集合的所有索引

<?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"
}

返回

insertOne()