|
| 1 | +.. _kotlin-sync-compound-index: |
| 2 | + |
| 3 | +================ |
| 4 | +Compound 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: index, query, optimization, efficiency |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +:manual:`Compound indexes </core/index-compound/>` hold references to multiple |
| 24 | +fields within a collection's documents, improving query and sort performance. |
| 25 | + |
| 26 | +When creating a compound index, you must specify the following: |
| 27 | + |
| 28 | +- The fields on which to create the index |
| 29 | +- The sort order for each field (ascending or descending) |
| 30 | + |
| 31 | +Sample Data |
| 32 | +~~~~~~~~~~~ |
| 33 | + |
| 34 | +The examples in this guide use the ``movies`` collection in the ``sample_mflix`` |
| 35 | +database from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a |
| 36 | +free MongoDB Atlas cluster and load the sample datasets, see the |
| 37 | +:atlas:`Get Started with Atlas </getting-started>` guide. |
| 38 | + |
| 39 | +The following {+language+} data class models the documents in this collection: |
| 40 | + |
| 41 | +.. literalinclude:: /includes/indexes/indexes.kt |
| 42 | + :start-after: start-movie-class |
| 43 | + :end-before: end-movie-class |
| 44 | + :language: kotlin |
| 45 | + :copyable: |
| 46 | + |
| 47 | +Create a Compound Index |
| 48 | +----------------------- |
| 49 | + |
| 50 | +The following example creates a compound index on the ``type`` and ``genre`` fields, with |
| 51 | +both fields indexed in ascending order: |
| 52 | + |
| 53 | +.. literalinclude:: /includes/indexes/indexes.kt |
| 54 | + :start-after: start-index-compound |
| 55 | + :end-before: end-index-compound |
| 56 | + :language: kotlin |
| 57 | + :copyable: |
| 58 | + :dedent: |
| 59 | + |
| 60 | +The following is an example of a query that uses the index created in |
| 61 | +the preceding code sample: |
| 62 | + |
| 63 | +.. io-code-block:: |
| 64 | + :copyable: true |
| 65 | + |
| 66 | + .. input:: /includes/indexes/indexes.kt |
| 67 | + :start-after: start-index-compound-query |
| 68 | + :end-before: end-index-compound-query |
| 69 | + :language: kotlin |
| 70 | + :dedent: |
| 71 | + |
| 72 | + .. output:: |
| 73 | + :visible: false |
| 74 | + |
| 75 | + Movie(id=573a1392f29313caabcda755, title=China Seas, type=movie, genres=[Action, Drama, Adventure], ...) |
| 76 | + Movie(id=573a1392f29313caabcd9ca6, title=Scarface, type=movie, genres=[Action, Crime, Drama], ... ) |
| 77 | + Movie(id=573a1392f29313caabcdb258, title=The Hurricane, type=movie, genres=[Action, Drama, Romance], ...) |
| 78 | + Movie(id=573a1391f29313caabcd820b, title=Beau Geste, type=movie, genres=[Action, Adventure, Drama], ...) |
| 79 | + ... |
| 80 | + |
| 81 | +Additional Information |
| 82 | +---------------------- |
| 83 | + |
| 84 | +To learn more about compound indexes, see :manual:`Compound Indexes </core/index-compound>` |
| 85 | +in the {+mdb-server+} manual. |
| 86 | + |
| 87 | +To learn about effective indexing strategies using compound indexes, see |
| 88 | +:manual:`The ESR Rule </tutorial/equality-sort-range-rule>` in the {+mdb-server+} manual. |
| 89 | + |
| 90 | +API Documentation |
| 91 | +~~~~~~~~~~~~~~~~~ |
| 92 | + |
| 93 | +To learn more about any of the methods discussed in this guide, see the following API |
| 94 | +documentation: |
| 95 | + |
| 96 | +- `find() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/find.html>`__ |
| 97 | +- `filter() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-find-iterable/filter.html>`__ |
| 98 | +- `sort() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-find-iterable/sort.html>`__ |
0 commit comments