|
| 1 | +.. _scala-indexes: |
| 2 | + |
| 3 | +============== |
| 4 | +Create Indexes |
| 5 | +============== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: code example, optimize, covered query |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Indexes support the efficient execution of queries in MongoDB. To |
| 21 | +create an index on a field or fields, pass an index specification |
| 22 | +document to the ``MongoCollection.createIndex()`` method. |
| 23 | + |
| 24 | +The {+driver-short+} provides the ``Indexes`` class that includes |
| 25 | +static factory methods to create index specification documents for the |
| 26 | +various MongoDB index key types. To learn more about index types, see |
| 27 | +:manual:`Indexes </indexes/>` in the Server manual. |
| 28 | + |
| 29 | +.. note:: |
| 30 | + |
| 31 | + MongoDB only creates an index if an index of the same specification |
| 32 | + does not already exist. |
| 33 | + |
| 34 | +Prerequisites |
| 35 | +------------- |
| 36 | + |
| 37 | +You must include the following import statements in your program to run the |
| 38 | +code examples in this guide: |
| 39 | + |
| 40 | +.. code-block:: scala |
| 41 | + |
| 42 | + import org.mongodb.scala._ |
| 43 | + |
| 44 | + import org.mongodb.Indexes |
| 45 | + import org.mongodb.IndexOptions |
| 46 | + import org.mongodb.Filters |
| 47 | + |
| 48 | +.. include:: /includes/obs-note.rst |
| 49 | + |
| 50 | +Connect to a MongoDB Deployment |
| 51 | +------------------------------- |
| 52 | + |
| 53 | +.. include:: /includes/connect-section.rst |
| 54 | + |
| 55 | +Ascending Index |
| 56 | +--------------- |
| 57 | + |
| 58 | +To create a specification for an ascending index, use the |
| 59 | +``Indexes.ascending()`` static helper method. |
| 60 | + |
| 61 | +Single Ascending Index |
| 62 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 63 | + |
| 64 | +The following example creates an ascending index on the ``name`` field: |
| 65 | + |
| 66 | +.. code-block:: scala |
| 67 | + |
| 68 | + collection.createIndex(Indexes.ascending("name")) |
| 69 | + .printResults() |
| 70 | + |
| 71 | +Compound Ascending Index |
| 72 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 73 | + |
| 74 | +The following example creates an ascending compound index on the |
| 75 | +``stars`` field and the ``name`` field: |
| 76 | + |
| 77 | +.. code-block:: scala |
| 78 | + |
| 79 | + collection.createIndex(Indexes.ascending("stars", "name")) |
| 80 | + .printResults() |
| 81 | + |
| 82 | +To view an alternative way to create a compound index, see the :ref:`Compound |
| 83 | +Indexes <scala-compound-indexes>` section. |
| 84 | + |
| 85 | +Descending Index |
| 86 | +---------------- |
| 87 | + |
| 88 | +To create a specification of a descending index, use the |
| 89 | +``Indexes.descending()`` static helper method. |
| 90 | + |
| 91 | +Single Descending Key Index |
| 92 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 93 | + |
| 94 | +The following example creates a descending index on the ``stars`` field: |
| 95 | + |
| 96 | +.. code-block:: scala |
| 97 | + |
| 98 | + collection.createIndex(Indexes.descending("stars")) |
| 99 | + .printResults() |
| 100 | + |
| 101 | +Compound Descending Key Index |
| 102 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 103 | + |
| 104 | +The following example creates a descending compound index on the |
| 105 | +``stars`` field and the ``name`` field: |
| 106 | + |
| 107 | +.. code-block:: scala |
| 108 | + |
| 109 | + collection.createIndex(Indexes.descending("stars", "name")) |
| 110 | + .printResults() |
| 111 | + |
| 112 | +To view an alternative way to create a compound index, see the :ref:`Compound |
| 113 | +Indexes <scala-compound-indexes>` section. |
| 114 | + |
| 115 | +.. _scala-compound-indexes: |
| 116 | + |
| 117 | +Compound Indexes |
| 118 | +---------------- |
| 119 | + |
| 120 | +To create a specification for a compound index, use the |
| 121 | +``Indexes.compoundIndex()`` static helper method. |
| 122 | + |
| 123 | +.. note:: |
| 124 | + |
| 125 | + To create a specification for a compound index where all the keys are |
| 126 | + ascending, you can use the ``ascending()`` method. To create a |
| 127 | + specification for a compound index where all the keys are descending, |
| 128 | + you can use the ``descending()`` method. |
| 129 | + |
| 130 | +The following example creates a compound index on the ``stars`` field |
| 131 | +in descending order and the ``name`` field in ascending order: |
| 132 | + |
| 133 | +.. code-block:: scala |
| 134 | + |
| 135 | + collection.createIndex( |
| 136 | + Indexes.compoundIndex(Indexes.descending("stars"), |
| 137 | + Indexes.ascending("name"))) |
| 138 | + .printResults() |
| 139 | + |
| 140 | +Text Indexes |
| 141 | +------------ |
| 142 | + |
| 143 | +MongoDB provides text indexes to support text search of string |
| 144 | +content. Text indexes can include any field whose value is a string or |
| 145 | +an array of string elements. To create a specification for a text |
| 146 | +index, use the ``Indexes.text()`` helper method. |
| 147 | + |
| 148 | +The following example creates a text index on the ``name`` field: |
| 149 | + |
| 150 | +.. code-block:: scala |
| 151 | + |
| 152 | + collection.createIndex(Indexes.text("name")) |
| 153 | + .printResults() |
| 154 | + |
| 155 | +To learn more about text indexes, see |
| 156 | +:manual:`Text Indexes </core/indexes/index-types/index-text/>` in the Server manual. |
| 157 | + |
| 158 | +Hashed Index |
| 159 | +------------ |
| 160 | + |
| 161 | +To create a specification for a hashed index index, use the |
| 162 | +``Indexes.hashed()`` static helper method. |
| 163 | + |
| 164 | +The following example creates a hashed index on the ``_id`` field: |
| 165 | + |
| 166 | +.. code-block:: scala |
| 167 | + |
| 168 | + collection.createIndex(Indexes.hashed("_id")) |
| 169 | + .printResults() |
| 170 | + |
| 171 | +To learn more about hashed indexes, see |
| 172 | +:manual:`Hashed Indexes </core/indexes/index-types/index-hashed/>` in the Server manual. |
| 173 | + |
| 174 | +Geospatial Indexes |
| 175 | +------------------ |
| 176 | + |
| 177 | +To support geospatial queries, MongoDB supports various geospatial |
| 178 | +indexes. To learn more about geospatial indexes, see |
| 179 | +:manual:`Geospatial Indexes </core/indexes/index-types/index-geospatial/>` in the Server manual. |
| 180 | + |
| 181 | +2dsphere |
| 182 | +~~~~~~~~ |
| 183 | + |
| 184 | +To create a specification for a ``2dsphere`` index, use the |
| 185 | +``Indexes.geo2dsphere()`` static helper method. |
| 186 | + |
| 187 | +The following example creates a ``2dsphere`` index on the |
| 188 | +``contact.location`` field: |
| 189 | + |
| 190 | +.. code-block:: scala |
| 191 | + |
| 192 | + collection.createIndex(Indexes.geo2dsphere("contact.location")) |
| 193 | + .printResults() |
| 194 | + |
| 195 | +IndexOptions |
| 196 | +------------ |
| 197 | + |
| 198 | +In addition to the index specification document, the |
| 199 | +``createIndex()`` method can take an index options document that |
| 200 | +directs the driver to create unique indexes or partial indexes. |
| 201 | + |
| 202 | +The driver provides the ``IndexOptions`` class to specify various |
| 203 | +index options. |
| 204 | + |
| 205 | +Add the following import statement to your code to create an |
| 206 | +``IndexOptions`` instance. |
| 207 | + |
| 208 | +.. code-block:: scala |
| 209 | + |
| 210 | + import org.mongodb.scala.model.IndexOptions |
| 211 | + |
| 212 | +Unique Index |
| 213 | +~~~~~~~~~~~~ |
| 214 | + |
| 215 | +The following code specifies the ``unique(true)`` option to create a |
| 216 | +unique index on the ``name`` and ``stars`` fields: |
| 217 | + |
| 218 | +.. code-block:: scala |
| 219 | + |
| 220 | + val indexOptions = IndexOptions().unique(true) |
| 221 | + collection.createIndex(Indexes.ascending("name", "stars"), indexOptions) |
| 222 | + .printResults() |
| 223 | + |
| 224 | +To learn more about unique indexes, see |
| 225 | +:manual:`Unique Indexes </core/index-unique/>` in the Server manual. |
| 226 | + |
| 227 | +Partial Index |
| 228 | +~~~~~~~~~~~~~ |
| 229 | + |
| 230 | +To create a partial index, include the ``partialFilterExpression`` index |
| 231 | +option. |
| 232 | + |
| 233 | +The following example creates a partial index on documents in which the |
| 234 | +value of the ``status`` field is ``"A"``. |
| 235 | + |
| 236 | +.. code-block:: scala |
| 237 | + |
| 238 | + val partialFilterIndexOptions = IndexOptions() |
| 239 | + .partialFilterExpression(Filters.exists("contact.email")) |
| 240 | + collection.createIndex( |
| 241 | + Indexes.descending("name", "stars"), partialFilterIndexOptions) |
| 242 | + .printResults() |
| 243 | + |
| 244 | +To learn more about partial indexes, see |
| 245 | +:manual:`Partial Indexes </core/index-partial/>` in the Server manual. |
| 246 | + |
| 247 | +Get a List of Indexes on a Collection |
| 248 | +------------------------------------- |
| 249 | + |
| 250 | +Use the ``listIndexes()`` method to get a list of indexes. The following code |
| 251 | +lists the indexes on the collection: |
| 252 | + |
| 253 | +.. code-block:: scala |
| 254 | + |
| 255 | + collection.listIndexes().printResults() |
| 256 | + |
| 257 | +To learn about other index options, see :manual:`Index Properties |
| 258 | +</core/indexes/index-properties/>` in the Server manual. |
0 commit comments