|
| 1 | +.. _php-indexes: |
| 2 | + |
| 3 | +================================= |
| 4 | +Optimize Queries by Using Indexes |
| 5 | +================================= |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :description: Learn how to use indexes by using the MongoDB PHP Library. |
| 19 | + :keywords: query, optimization, efficiency, usage example, code example |
| 20 | + |
| 21 | +.. .. toctree:: |
| 22 | +.. :titlesonly: |
| 23 | +.. :maxdepth: 1 |
| 24 | +.. |
| 25 | +.. /work-with-indexes |
| 26 | + |
| 27 | +Overview |
| 28 | +-------- |
| 29 | + |
| 30 | +On this page, you can see copyable code examples that show how to manage |
| 31 | +different types of indexes by using the {+php-library+}. |
| 32 | + |
| 33 | +.. .. tip:: |
| 34 | +.. |
| 35 | +.. To learn more about working with indexes, see the :ref:`php-work-indexes` |
| 36 | +.. guide. To learn more about any of the indexes shown on this page, see the link |
| 37 | +.. provided in each section. |
| 38 | + |
| 39 | +To use an example from this page, copy the code example into the |
| 40 | +:ref:`sample application <php-index-sample>` or your own application. |
| 41 | +Be sure to replace all placeholders in the code examples, such as |
| 42 | +``<connection string>``, with the relevant values for your MongoDB |
| 43 | +deployment. |
| 44 | + |
| 45 | +.. _php-index-sample: |
| 46 | + |
| 47 | +.. include:: /includes/usage-examples/sample-app-intro.rst |
| 48 | + |
| 49 | +.. literalinclude:: /includes/usage-examples/sample-app.php |
| 50 | + :language: php |
| 51 | + :copyable: |
| 52 | + :linenos: |
| 53 | + :emphasize-lines: 10-12 |
| 54 | + |
| 55 | +Single Field Index |
| 56 | +------------------ |
| 57 | + |
| 58 | +The following example creates an ascending index on the specified field: |
| 59 | + |
| 60 | +.. literalinclude:: /includes/usage-examples/index-code-examples.php |
| 61 | + :start-after: start-single-field |
| 62 | + :end-before: end-single-field |
| 63 | + :language: php |
| 64 | + :copyable: |
| 65 | + :dedent: |
| 66 | + |
| 67 | +.. To learn more about single field indexes, see the |
| 68 | +.. :ref:`php-single-field-index` guide. |
| 69 | + |
| 70 | +Compound Index |
| 71 | +-------------- |
| 72 | + |
| 73 | +The following example creates a compound index on the specified fields: |
| 74 | + |
| 75 | +.. literalinclude:: /includes/usage-examples/index-code-examples.php |
| 76 | + :start-after: start-compound |
| 77 | + :end-before: end-compound |
| 78 | + :language: php |
| 79 | + :copyable: |
| 80 | + :dedent: |
| 81 | + |
| 82 | +.. To learn more about compound indexes, see the :ref:`php-compound-index` guide. |
| 83 | + |
| 84 | +Multikey Index |
| 85 | +-------------- |
| 86 | + |
| 87 | +The following example creates a multikey index on the specified array-valued field: |
| 88 | + |
| 89 | +.. literalinclude:: /includes/usage-examples/index-code-examples.php |
| 90 | + :start-after: start-multikey |
| 91 | + :end-before: end-multikey |
| 92 | + :language: php |
| 93 | + :copyable: |
| 94 | + :dedent: |
| 95 | + |
| 96 | +.. TODO To learn more about multikey indexes, see the :ref:`php-multikey-index` |
| 97 | +.. guide. |
| 98 | + |
| 99 | +Geospatial Index |
| 100 | +---------------- |
| 101 | + |
| 102 | +The following example creates a 2dsphere index on the specified field |
| 103 | +that has GeoJSON object values: |
| 104 | + |
| 105 | +.. literalinclude:: /includes/usage-examples/index-code-examples.php |
| 106 | + :start-after: start-geo |
| 107 | + :end-before: end-geo |
| 108 | + :language: php |
| 109 | + :copyable: |
| 110 | + :dedent: |
| 111 | + |
| 112 | +.. TODO: To learn more about geospatial indexes, see the :ref:`php-geospatial-index` |
| 113 | +.. guide. |
| 114 | + |
| 115 | +Unique Index |
| 116 | +------------ |
| 117 | + |
| 118 | +The following example creates a unique index on the specified field: |
| 119 | + |
| 120 | +.. literalinclude:: /includes/usage-examples/index-code-examples.php |
| 121 | + :start-after: start-unique |
| 122 | + :end-before: end-unique |
| 123 | + :language: php |
| 124 | + :copyable: |
| 125 | + :dedent: |
| 126 | + |
| 127 | +.. TODO: To learn more about unique indexes, see the :ref:`php-unique-index` |
| 128 | +.. guide. |
| 129 | + |
| 130 | +Wildcard Index |
| 131 | +-------------- |
| 132 | + |
| 133 | +The following example creates a wildcard index in the specified collection: |
| 134 | + |
| 135 | +.. literalinclude:: /includes/usage-examples/index-code-examples.php |
| 136 | + :start-after: start-wildcard |
| 137 | + :end-before: end-wildcard |
| 138 | + :language: php |
| 139 | + :copyable: |
| 140 | + :dedent: |
| 141 | + |
| 142 | +.. TODO: To learn more about wildcard indexes, see the :ref:`php-wildcard-index` |
| 143 | +.. guide. |
| 144 | + |
| 145 | +Clustered Index |
| 146 | +--------------- |
| 147 | + |
| 148 | +You can create a clustered index when creating a new collection in a |
| 149 | +specified database. The following example creates a new collection with a |
| 150 | +clustered index on the ``_id`` field: |
| 151 | + |
| 152 | +.. literalinclude:: /includes/usage-examples/index-code-examples.php |
| 153 | + :start-after: start-clustered |
| 154 | + :end-before: end-clustered |
| 155 | + :language: php |
| 156 | + :copyable: |
| 157 | + :dedent: |
| 158 | + |
| 159 | +.. TODO: To learn more about clustered indexes, see the :ref:`php-clustered-index` |
| 160 | +.. guide. |
| 161 | + |
| 162 | +Atlas Search Index Management |
| 163 | +----------------------------- |
| 164 | + |
| 165 | +The following sections contain code examples that describe how to manage |
| 166 | +:atlas:`Atlas Search indexes </atlas-search/manage-indexes/>`. |
| 167 | + |
| 168 | +.. To learn more about Atlas Search indexes, see the :ref:`php-atlas-search-index` |
| 169 | +.. guide. |
| 170 | + |
| 171 | +Create Search Index |
| 172 | +~~~~~~~~~~~~~~~~~~~ |
| 173 | + |
| 174 | +The following example creates an Atlas Search index on the specified field: |
| 175 | + |
| 176 | +.. literalinclude:: /includes/usage-examples/index-code-examples.php |
| 177 | + :start-after: start-search-create |
| 178 | + :end-before: end-search-create |
| 179 | + :language: php |
| 180 | + :copyable: |
| 181 | + :dedent: |
| 182 | + |
| 183 | +.. To learn more about creating search indexes, see the |
| 184 | +.. :ref:`php-atlas-search-index-create` guide. |
| 185 | + |
| 186 | +List Search Indexes |
| 187 | +~~~~~~~~~~~~~~~~~~~ |
| 188 | + |
| 189 | +The following example prints a list of Atlas Search indexes in the specified collection: |
| 190 | + |
| 191 | +.. literalinclude:: /includes/usage-examples/index-code-examples.php |
| 192 | + :start-after: start-search-list |
| 193 | + :end-before: end-search-list |
| 194 | + :language: php |
| 195 | + :copyable: |
| 196 | + :dedent: |
| 197 | + |
| 198 | +.. To learn more about listing search indexes, see the :ref:`php-atlas-search-index-list` |
| 199 | +.. guide. |
| 200 | + |
| 201 | +Update Search Indexes |
| 202 | +~~~~~~~~~~~~~~~~~~~~~ |
| 203 | + |
| 204 | +The following example updates an existing Atlas Search index with the specified |
| 205 | +new index definition: |
| 206 | + |
| 207 | +.. literalinclude:: /includes/usage-examples/index-code-examples.php |
| 208 | + :start-after: start-search-update |
| 209 | + :end-before: end-search-update |
| 210 | + :language: php |
| 211 | + :copyable: |
| 212 | + :dedent: |
| 213 | + |
| 214 | +.. To learn more about updating search indexes, see the :ref:`php-atlas-search-index-update` |
| 215 | +.. guide. |
| 216 | + |
| 217 | +Delete Search Indexes |
| 218 | +~~~~~~~~~~~~~~~~~~~~~ |
| 219 | + |
| 220 | +The following example deletes an Atlas Search index with the specified name: |
| 221 | + |
| 222 | +.. literalinclude:: /includes/usage-examples/index-code-examples.php |
| 223 | + :start-after: start-search-delete |
| 224 | + :end-before: end-search-delete |
| 225 | + :language: php |
| 226 | + :copyable: |
| 227 | + :dedent: |
| 228 | + |
| 229 | +.. To learn more about deleting search indexes, see the :ref:`php-atlas-search-index-drop` |
| 230 | +.. guide. |
| 231 | + |
| 232 | +Text Index |
| 233 | +---------- |
| 234 | + |
| 235 | +The following example creates a text index on the specified string field: |
| 236 | + |
| 237 | +.. literalinclude:: /includes/usage-examples/index-code-examples.php |
| 238 | + :start-after: start-text |
| 239 | + :end-before: end-text |
| 240 | + :language: php |
| 241 | + :copyable: |
| 242 | + :dedent: |
| 243 | + |
| 244 | +.. TODO: To learn more about text indexes, see the :ref:`php-text-index` |
| 245 | +.. guide. |
| 246 | + |
| 247 | +Delete an Index |
| 248 | +--------------- |
| 249 | + |
| 250 | +The following example deletes an index with the specified name: |
| 251 | + |
| 252 | +.. literalinclude:: /includes/usage-examples/index-code-examples.php |
| 253 | + :start-after: start-remove |
| 254 | + :end-before: end-remove |
| 255 | + :language: php |
| 256 | + :copyable: |
| 257 | + :dedent: |
| 258 | + |
| 259 | +.. TODO: To learn more about removing indexes, see :ref:`php-indexes-remove` |
| 260 | +.. in the Work with Indexes guide. |
0 commit comments