|
| 1 | +.. _ruby-multikey-index: |
| 2 | + |
| 3 | +================ |
| 4 | +Multikey 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 | +**Multikey indexes** are indexes that improve the performance of queries |
| 24 | +on array-valued fields. You can create a multikey index on a collection |
| 25 | +by using the ``create_one`` method and the same syntax that you use to create |
| 26 | +a :ref:`single field index <ruby-single-field-index>`. |
| 27 | + |
| 28 | + |
| 29 | +When creating a multikey index, you must specify the following details: |
| 30 | + |
| 31 | +- The fields on which to create the index |
| 32 | + |
| 33 | +- The sort order for each field (ascending or descending) |
| 34 | + |
| 35 | +Sample Data |
| 36 | +~~~~~~~~~~~ |
| 37 | + |
| 38 | +The examples in this guide use the ``movies`` collection in the |
| 39 | +``sample_mflix`` database from the :atlas:`Atlas sample datasets |
| 40 | +</sample-data>`. To access this collection from your {+language+} |
| 41 | +application, create a ``Mongo::Client`` object that connects to |
| 42 | +an Atlas cluster and assign the following values to your ``database`` |
| 43 | +and ``collection`` variables: |
| 44 | + |
| 45 | +.. literalinclude:: /includes/indexes/single-field.rb |
| 46 | + :start-after: start-sample-data |
| 47 | + :end-before: end-sample-data |
| 48 | + :language: ruby |
| 49 | + :copyable: |
| 50 | + |
| 51 | +To learn how to create a free MongoDB Atlas cluster and |
| 52 | +load the sample datasets, see the :atlas:`Get Started with Atlas |
| 53 | +</getting-started>` guide. |
| 54 | + |
| 55 | +Create a Multikey Index |
| 56 | +----------------------- |
| 57 | + |
| 58 | +Use the ``create_one`` method to create a multikey index. The following example |
| 59 | +creates an index in ascending order on the ``cast`` field: |
| 60 | + |
| 61 | +.. literalinclude:: /includes/indexes/multikey.rb |
| 62 | + :start-after: start-index-multikey |
| 63 | + :end-before: end-index-multikey |
| 64 | + :language: ruby |
| 65 | + :copyable: |
| 66 | + |
| 67 | +Verify Index Creation |
| 68 | +--------------------- |
| 69 | + |
| 70 | +You can verify that the index was created by listing the indexes in the |
| 71 | +collection. You should see an index for ``cast`` in the list, as shown |
| 72 | +in the following output: |
| 73 | + |
| 74 | +.. io-code-block:: |
| 75 | + :copyable: true |
| 76 | + |
| 77 | + .. input:: /includes/indexes/multikey.rb |
| 78 | + :start-after: start-check-multikey-index |
| 79 | + :end-before: end-check-multikey-index |
| 80 | + :language: ruby |
| 81 | + |
| 82 | + .. output:: |
| 83 | + :visible: true |
| 84 | + |
| 85 | + {"v": 2, "key": {"cast": 1}, "name": "cast_1"} |
| 86 | + |
| 87 | +Example Query |
| 88 | +------------- |
| 89 | + |
| 90 | +The following is an example of a query that is covered by the index |
| 91 | +created on the ``cast`` field: |
| 92 | + |
| 93 | +.. io-code-block:: |
| 94 | + :copyable: true |
| 95 | + |
| 96 | + .. input:: /includes/indexes/multikey.rb |
| 97 | + :start-after: start-index-multikey-query |
| 98 | + :end-before: end-index-multikey-query |
| 99 | + :language: ruby |
| 100 | + |
| 101 | + .. output:: |
| 102 | + :visible: false |
| 103 | + |
| 104 | + {"_id":...,"title":"Fanaa",...,"cast": ["Aamir Khan", "Kajol", "Rishi Kapoor", "Tabu"],...} |
| 105 | + |
| 106 | +Additional Information |
| 107 | +---------------------- |
| 108 | + |
| 109 | +To view runnable examples that demonstrate how to manage indexes, see |
| 110 | +:ref:`ruby-indexes`. |
| 111 | + |
| 112 | +To learn more about multikey indexes, see :manual:`Multikey |
| 113 | +Indexes </core/indexes/index-types/index-multikey/>` in the {+mdb-server+} manual. |
| 114 | + |
| 115 | +API Documentation |
| 116 | +~~~~~~~~~~~~~~~~~ |
| 117 | + |
| 118 | +To learn more about any of the methods discussed in this guide, see the |
| 119 | +following API documentation: |
| 120 | + |
| 121 | +- `indexes <{+api-root+}/Mongo/Collection.html#indexes-instance_method>`__ |
| 122 | +- `create_one <{+api-root+}/Mongo/Index/View.html>`__ |
| 123 | +- `find <{+api-root+}/Mongo/Collection.html#find-instance_method>`__ |
0 commit comments