MongoDBCollection-listIndexes.txt 2.44 KB
Newer Older
1 2 3 4 5 6
==================================
MongoDB\\Collection::listIndexes()
==================================

.. default-domain:: mongodb

7
.. contents:: On this page
8 9 10 11 12 13 14 15
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

16 17 18
.. phpmethod:: MongoDB\\Collection::listIndexes()

   Returns information for all indexes for this collection.
19 20 21 22 23

   .. code-block:: php

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

24
   This method has the following parameters:
25

26
   .. include:: /includes/apiargs/MongoDBCollection-method-listIndexes-param.rst
27

28
   The ``$options`` parameter supports the following options:
29

30 31
   .. include:: /includes/apiargs/MongoDBCollection-method-listIndexes-option.rst

32 33
Return Values
-------------
34

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

38 39 40 41 42 43
Errors/Exceptions
-----------------

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

44 45 46
Example
-------

47
The following example lists all of the indexes for the ``restaurants``
48
collection in the ``test`` database:
49 50 51 52 53

.. code-block:: php

   <?php

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

56
   foreach ($collection->listIndexes() as $index) {
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
      var_dump($index);
   }

The output would then resemble::

   object(MongoDB\Model\IndexInfo)#8 (4) {
     ["v"]=>
     int(1)
     ["key"]=>
     array(1) {
       ["_id"]=>
       int(1)
     }
     ["name"]=>
     string(4) "_id_"
     ["ns"]=>
73
     string(16) "test.restaurants"
74 75 76 77 78 79 80 81 82 83 84 85
   }
   object(MongoDB\Model\IndexInfo)#12 (4) {
     ["v"]=>
     int(1)
     ["key"]=>
     array(1) {
       ["cuisine"]=>
       float(-1)
     }
     ["name"]=>
     string(10) "cuisine_-1"
     ["ns"]=>
86
     string(16) "test.restaurants"
87 88 89 90 91 92 93 94 95 96 97 98
   }
   object(MongoDB\Model\IndexInfo)#8 (4) {
     ["v"]=>
     int(1)
     ["key"]=>
     array(1) {
       ["borough"]=>
       float(1)
     }
     ["name"]=>
     string(9) "borough_1"
     ["ns"]=>
99
     string(16) "test.restaurants"
100 101
   }

102 103 104 105 106 107 108 109 110 111
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