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