MongoDBCollection-createIndex.txt 2.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
================================
MongoDBCollection::createIndex()
================================

.. default-domain:: mongodb

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

.. phpmethod:: MongoDB\\Collection::createIndex($key, $options)
               
   Create an index for the collection.
   
   .. code-block:: php
                   
      function createIndex($key, array $options = []): string

   ``createIndex()`` accepts the following parameters:
   
   .. include:: /includes/apiargs/MongoDBCollection-method-createIndex-param.rst

   The ``$options`` parameter accepts all index options that your
   MongoDB version supports. MongoDB 3.2 includes the following options:
                
   .. include:: /includes/apiargs/MongoDBCollection-method-createIndex-option.rst

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

Output
------

Returns the name of the created index as a string.
                
Examples
--------

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

The following operation creates a :manual:`compound index
</core/index-compound>` on the ``borough`` and ``cuisine`` fields
in the ``restaurants`` collection.

.. code-block:: php
                
   <?php
   
   $database = new MongoDB\Client;

   $collection = $database->selectCollection('example','restaurants');

   $keys = ['borough' => 1, 'cuisine' => 1];

   $indexString = $collection->createIndex($keys, ['name' => 'compound']);

   var_dump($indexString);

The output would resemble the following:

.. code-block:: none
                
   string(8) "compound"

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

The following operation adds a :manual:`partial index
</core/index-parital>` on the ``borough`` field in the ``restaurants``
collection in the ``example`` database. The partial index indexes only
fields where the ``borough`` field exists.

.. code-block:: php
                
   <?php
   
   $database = new MongoDB\Client;

   $collection = $database->selectCollection('example','restaurants');

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

The output would resemble::
                
   string(9) "borough_1"

.. seealso::
   
   - :phpmethod:`MongoDB\\Collection::createIndexes`
   - :doc:`/tutorial/indexes`
   - :manual:`createIndexes </reference/command/createIndexes>` 
     command reference in the MongoDB manual
   - :manual:`Index documentation </indexes>`