|
| 1 | +.. _ruby-single-field-index: |
| 2 | + |
| 3 | +==================== |
| 4 | +Single Field 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 | +Single field indexes are indexes with a reference to a single field of a |
| 24 | +document in a collection. These indexes improve single field query and |
| 25 | +sort performance. They also support :manual:`TTL Indexes </core/index-ttl>` |
| 26 | +that automatically remove documents from a collection after a certain |
| 27 | +amount of time or at a specified clock time. |
| 28 | + |
| 29 | +When creating a single field index, you must specify the following |
| 30 | +details: |
| 31 | + |
| 32 | +- The field on which to create the index |
| 33 | +- The sort order for the indexed values as either ascending or |
| 34 | + descending |
| 35 | + |
| 36 | +.. note:: |
| 37 | + |
| 38 | + The default ``_id_`` index is an example of a single field index. |
| 39 | + This index is automatically created on the ``_id`` field when a new |
| 40 | + collection is created. |
| 41 | + |
| 42 | +Sample Data |
| 43 | +~~~~~~~~~~~ |
| 44 | + |
| 45 | +The examples in this guide use the ``movies`` collection in the |
| 46 | +``sample_mflix`` database from the :atlas:`Atlas sample datasets |
| 47 | +</sample-data>`. To access this collection from your {+language+} |
| 48 | +application, create a ``Mongo::Client`` object that connects to |
| 49 | +an Atlas cluster and assign the following values to your ``database`` |
| 50 | +and ``collection`` |
| 51 | +variables: |
| 52 | + |
| 53 | +.. literalinclude:: /includes/indexes/single-field.rb |
| 54 | + :start-after: start-sample-data |
| 55 | + :end-before: end-sample-data |
| 56 | + :language: ruby |
| 57 | + :copyable: |
| 58 | + |
| 59 | +To learn how to create a free MongoDB Atlas cluster and |
| 60 | +load the sample datasets, see the :atlas:`Get Started with Atlas |
| 61 | +</getting-started>` guide. |
| 62 | + |
| 63 | +Create a Single Field Index |
| 64 | +--------------------------- |
| 65 | + |
| 66 | +Use the ``create_one`` method to create a single |
| 67 | +field index. The following example creates an index in ascending order on the |
| 68 | +``title`` field: |
| 69 | + |
| 70 | +.. literalinclude:: /includes/indexes/single-field.rb |
| 71 | + :start-after: start-index-single |
| 72 | + :end-before: end-index-single |
| 73 | + :language: ruby |
| 74 | + :copyable: |
| 75 | + |
| 76 | +Verify Index Creation |
| 77 | +--------------------- |
| 78 | + |
| 79 | +You can verify that the index was created by listing the indexes in the |
| 80 | +collection. You should see an index for ``title`` in the list, as shown |
| 81 | +in the following output: |
| 82 | + |
| 83 | +.. io-code-block:: |
| 84 | + :copyable: true |
| 85 | + |
| 86 | + .. input:: /includes/indexes/single-field.rb |
| 87 | + :start-after: start-check-single-index |
| 88 | + :end-before: end-check-single-index |
| 89 | + :language: ruby |
| 90 | + :dedent: |
| 91 | + |
| 92 | + .. output:: |
| 93 | + :visible: true |
| 94 | + |
| 95 | + {"v": 2, "key": {"title": 1}, "name": "title_1"} |
| 96 | + |
| 97 | +Example Query |
| 98 | +------------- |
| 99 | + |
| 100 | +The following is an example of a query that is covered by the index |
| 101 | +created on the ``title`` field: |
| 102 | + |
| 103 | +.. io-code-block:: |
| 104 | + :copyable: true |
| 105 | + |
| 106 | + .. input:: /includes/indexes/single-field.rb |
| 107 | + :start-after: start-index-single-query |
| 108 | + :end-before: end-index-single-query |
| 109 | + :language: ruby |
| 110 | + :dedent: |
| 111 | + |
| 112 | + .. output:: |
| 113 | + :visible: false |
| 114 | + |
| 115 | + {"_id":...,"plot":"A musical comedy duo...", |
| 116 | + "genres":["Musical"],...,"title":"Sweethearts",...} |
| 117 | + |
| 118 | +Additional Information |
| 119 | +---------------------- |
| 120 | + |
| 121 | +To view runnable examples that demonstrate how to manage indexes, see |
| 122 | +:ref:`ruby-indexes`. |
| 123 | + |
| 124 | +To learn more about single field indexes, see :manual:`Single Field |
| 125 | +Indexes </core/index-single>` in the {+mdb-server+} manual. |
| 126 | + |
| 127 | +API Documentation |
| 128 | +~~~~~~~~~~~~~~~~~ |
| 129 | + |
| 130 | +To learn more about any of the methods discussed in this guide, see the |
| 131 | +following API documentation: |
| 132 | + |
| 133 | +- `indexes <{+api-root+}/Mongo/Collection.html#indexes-instance_method>`__ |
| 134 | +- `create_one <{+api-root+}/Mongo/Index/View.html>`__ |
| 135 | +- `find <{+api-root+}/Mongo/Collection.html#find-instance_method>`__ |
0 commit comments