MongoDBCollection-createIndex.txt 2.67 KB
Newer Older
1 2 3
==================================
MongoDB\\Collection::createIndex()
==================================
4 5 6 7 8 9 10 11

.. default-domain:: mongodb

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

13 14 15
Definition
----------

16 17
.. phpmethod:: MongoDB\\Collection::createIndex()

18
   Create an index for the collection.
19

20
   .. code-block:: php
21

22 23
      function createIndex($key, array $options = []): string

24
   This method has the following parameters:
25

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

28
   The ``$options`` parameter accepts all index options that your MongoDB
29
   version supports. MongoDB includes the following options:
30

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

33 34 35
   For a full list of the supported index creation options, refer to the
   :manual:`createIndexes </reference/command/createIndexes>` command reference
   in the MongoDB manual.
36

37 38
Return Values
-------------
39

40
The name of the created index as a string.
41

42 43 44 45 46 47 48
Errors/Exceptions
-----------------

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

49 50 51 52 53 54
Examples
--------

Create a Compound Index
~~~~~~~~~~~~~~~~~~~~~~~

55 56
The following example creates a :manual:`compound index </core/index-compound>`
on the ``borough`` and ``cuisine`` fields in the ``restaurants`` collection in
57
the ``test`` database.
58 59 60

.. code-block:: php

61
   <?php
62

63
   $collection = (new MongoDB\Client)->selectCollection('test', 'restaurants');
64

65
   $indexName = $collection->createIndex(['borough' => 1, 'cuisine' => 1]);
66

67
   var_dump($indexName);
68

69
The output would then resemble::
70

71
   string(19) "borough_1_cuisine_1"
72 73 74 75

Create a Partial Index
~~~~~~~~~~~~~~~~~~~~~~

76
The following example adds a :manual:`partial index </core/index-parital>` on
77
the ``borough`` field in the ``restaurants`` collection in the ``test``
78 79
database. The partial index indexes only documents where the ``borough`` field
exists.
80 81

.. code-block:: php
82

83
   <?php
84

85
   $collection = (new MongoDB\Client)->selectCollection('test, 'restaurants');
86 87 88 89 90 91 92 93 94 95 96 97 98 99

   $indexName = $collection->createIndex(
      ['borough' => 1],
      [
          'partialFilterExpression' => [
              'borough' => ['$exists' => true],
          ],
      ]
   );

   var_dump($indexName);

The output would then resemble::

100 101
   string(9) "borough_1"

102 103
See Also
--------
104

105 106 107 108 109
- :phpmethod:`MongoDB\\Collection::createIndexes()`
- :doc:`/tutorial/indexes`
- :manual:`createIndexes </reference/command/createIndexes>` command reference
  in the MongoDB manual
- :manual:`Index </indexes>` documentation in the MongoDB Manual