|
| 1 | +.. _kotlin-sync-work-with-indexes: |
| 2 | + |
| 3 | +================= |
| 4 | +Work with 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 | + :keywords: query, optimization, efficiency |
| 19 | + |
| 20 | +.. toctree:: |
| 21 | + |
| 22 | + /indexes/single-field-index.txt |
| 23 | + |
| 24 | +Overview |
| 25 | +-------- |
| 26 | + |
| 27 | +In this guide, you can learn how to use **indexes** with the {+driver-short+}. |
| 28 | +Indexes can improve the efficiency of queries and add additional functionality |
| 29 | +to querying and storing documents. |
| 30 | + |
| 31 | +Without indexes, MongoDB must scan every document in a collection to find the |
| 32 | +documents that match each query. These collection scans are slow and can negatively affect |
| 33 | +the performance of your application. However, if an appropriate index exists for a query, |
| 34 | +MongoDB can use the index to limit the documents it must inspect. |
| 35 | + |
| 36 | +Operational Considerations |
| 37 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 38 | + |
| 39 | +To improve query performance, build indexes on fields that appear often in |
| 40 | +your application's queries and operations that return sorted results. Each |
| 41 | +index that you add consumes disk space and memory when active, so we recommend |
| 42 | +that you track index memory and disk usage for capacity planning. In addition, |
| 43 | +when a write operation updates an indexed field, MongoDB updates the related |
| 44 | +index. |
| 45 | + |
| 46 | +Because MongoDB supports dynamic schemas, applications can query against fields |
| 47 | +whose names are not known in advance or are arbitrary. MongoDB 4.2 introduced |
| 48 | +:manual:`wildcard indexes </core/index-wildcard/>` to help support these |
| 49 | +queries. Wildcard indexes are not designed to replace workload-based index |
| 50 | +planning. |
| 51 | + |
| 52 | +For more information about designing your data model and choosing indexes appropriate for your application, see the |
| 53 | +:manual:`Data Modeling and Indexes </core/data-model-operations/#indexes>` guide |
| 54 | +in the {+mdb-server+} manual. |
| 55 | + |
| 56 | +Sample Data |
| 57 | +~~~~~~~~~~~ |
| 58 | + |
| 59 | +The examples in this guide use the ``movies`` collection in the ``sample_mflix`` |
| 60 | +database from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a |
| 61 | +free MongoDB Atlas cluster and load the sample datasets, see the |
| 62 | +:atlas:`Get Started with Atlas </getting-started>` guide. |
| 63 | + |
| 64 | +Create an Index |
| 65 | +--------------- |
| 66 | + |
| 67 | +MongoDB supports several different index types to support querying your data. |
| 68 | +The following pages describe the most common index types and provide sample |
| 69 | +code for creating each index type. |
| 70 | + |
| 71 | +- :ref:`kotlin-sync-single-field-index` |
| 72 | + |
| 73 | +.. TODO: - :ref:`kotlin-sync-compound-index` |
| 74 | +.. TODO: - :ref:`kotlin-sync-atlas-search-index` |
| 75 | + |
| 76 | +Remove an Index |
| 77 | +--------------- |
| 78 | + |
| 79 | +You can remove any unused index except the default unique index on the |
| 80 | +``_id`` field. |
| 81 | + |
| 82 | +The following sections show how to remove a single index or to remove all |
| 83 | +indexes in a collection. |
| 84 | + |
| 85 | +Delete a Single Index |
| 86 | +~~~~~~~~~~~~~~~~~~~~~ |
| 87 | + |
| 88 | +Pass an index name to the ``dropIndex()`` method to remove an index from a collection. |
| 89 | + |
| 90 | +The following example removes an index with the name ``"_title_"`` from the ``movies`` |
| 91 | +collection: |
| 92 | + |
| 93 | +.. literalinclude:: /includes/indexes/indexes.kt |
| 94 | + :language: kotlin |
| 95 | + :start-after: start-remove-index |
| 96 | + :end-before: end-remove-index |
| 97 | + |
| 98 | +.. note:: |
| 99 | + |
| 100 | + You cannot remove a single field from a compound text index. You must |
| 101 | + drop the entire index and create a new one to update the indexed |
| 102 | + fields. |
| 103 | + |
| 104 | +Delete All Indexes |
| 105 | +~~~~~~~~~~~~~~~~~~ |
| 106 | + |
| 107 | +Starting with MongoDB 4.2, you can drop all indexes by calling the |
| 108 | +``dropIndexes()`` method on your collection: |
| 109 | + |
| 110 | +.. code-block:: kotlin |
| 111 | + |
| 112 | + collection.dropIndexes() |
| 113 | + |
| 114 | +API Documentation |
| 115 | +~~~~~~~~~~~~~~~~~ |
| 116 | + |
| 117 | +To learn more about any of the methods or types discussed in this |
| 118 | +guide, see the following API documentation: |
| 119 | + |
| 120 | +- `createIndex() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/create-index.html>`__ |
| 121 | +- `createIndexes() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/create-indexes.html>`__ |
| 122 | +- `dropIndex() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/drop-index.html>`__ |
| 123 | +- `dropIndexes() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/drop-indexes.html>`__ |
0 commit comments