==================================
MongoDB\\Collection::listIndexes()
==================================

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

.. phpmethod:: MongoDB\Collection::listIndexes()

   Returns information for all indexes for this collection.

   .. code-block:: php

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

Parameters
----------

``$options`` : array
  An array specifying the desired options.

  .. list-table::
     :header-rows: 1
     :widths: 20 20 80

     * - Name
       - Type
       - Description

     * - comment
       - mixed
       - .. include:: /includes/extracts/common-option-comment.rst

         .. include:: /includes/extracts/option-requires-4.4.rst

         .. versionadded:: 1.13

     * - maxTimeMS
       - integer
       - .. include:: /includes/extracts/common-option-maxTimeMS.rst

     * - session
       - :php:`MongoDB\Driver\Session <class.mongodb-driver-session>`
       - .. include:: /includes/extracts/common-option-session.rst

         .. versionadded:: 1.3

Return Values
-------------

A traversable :phpclass:`MongoDB\Model\IndexInfoIterator`, which contains a
:phpclass:`MongoDB\Model\IndexInfo` object for each index for the collection.

Errors/Exceptions
-----------------

.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

Example
-------

The following example lists all of the indexes for the ``restaurants``
collection in the ``test`` database:

.. code-block:: php

   <?php

   $collection = (new MongoDB\Client)->test->restaurants;

   foreach ($collection->listIndexes() as $index) {
      var_dump($index);
   }

The output would then resemble:

.. code-block:: none

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

See Also
--------

- :doc:`/tutorial/indexes`
- :manual:`listIndexes </reference/command/listIndexes>` command reference in
  the MongoDB manual
- :manual:`Index documentation </core/indexes>` in the MongoDB manual
- `Enumerating Collections
  <https://github.com/mongodb/specifications/blob/master/source/enumerate-indexes.rst>`_
  specification
