MongoDBCollection-dropIndexes.txt 1.96 KB
Newer Older
1 2 3 4 5
==================================
MongoDB\\Collection::dropIndexes()
==================================

.. default-domain:: mongodb
6

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

Definition
----------

16 17 18 19 20
.. phpmethod:: MongoDB\\Collection::dropIndexes()

   Drop all indexes in the collection, except for the required index on the
   ``_id`` field.

21
   .. code-block:: php
22

23 24
      function dropIndexes(array $options = []): array|object

25
   This method has the following parameters:
26

27
   .. include:: /includes/apiargs/MongoDBCollection-method-dropIndex-param.rst
28 29 30

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

31 32
   .. include:: /includes/apiargs/MongoDBCollection-method-dropIndex-option.rst

33 34
Return Values
-------------
35

36 37 38
An array or object with the result document of the :manual:`dropIndexes
</reference/command/dropIndexes>` command. The return type will depend on the
``typeMap`` option.
39

40 41 42 43 44 45 46
Errors/Exceptions
-----------------

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

47 48 49
Example
-------

50
The following drops all indexes from the ``restaurants`` collection in the
51
``test`` database:
52 53 54 55 56

.. code-block:: php

   <?php

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

59
   $result = $collection->dropIndexes();
60

61
   var_dump($result);
62

63
The output would then resemble::
64 65 66 67 68 69 70 71 72 73 74 75 76

   object(MongoDB\Model\BSONDocument)#9 (1) {
     ["storage":"ArrayObject":private]=>
     array(3) {
       ["nIndexesWas"]=>
       int(3)
       ["msg"]=>
       string(38) "non-_id indexes dropped for collection"
       ["ok"]=>
       float(1)
     }
   }

77 78 79 80 81 82 83 84
See Also
--------

- :phpmethod:`MongoDB\\Collection::dropIndex()`
- :doc:`/tutorial/indexes`
- :manual:`dropIndexes </reference/command/dropIndexes>` command reference in
  the MongoDB manual
- :manual:`Index documentation </indexes>` in the MongoDB manual