|
| 1 | +.. _scala-builders-indexes: |
| 2 | + |
| 3 | +======= |
| 4 | +Indexes |
| 5 | +======= |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: code example, optimize, geospatial, text search |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +The ``Indexes`` class provides static factory methods for all the MongoDB index key types. |
| 21 | +Each method returns an instance of the ``Bson`` type, which can in turn be |
| 22 | +used with the ``createIndex()`` methods. |
| 23 | + |
| 24 | +You can import the methods of the ``Indexes`` |
| 25 | +class statically, as shown in the following code: |
| 26 | + |
| 27 | +.. code-block:: scala |
| 28 | + |
| 29 | + import org.mongodb.scala.model.Indexes._ |
| 30 | + |
| 31 | +The examples in this guide assume this static import. |
| 32 | + |
| 33 | +Ascending |
| 34 | +--------- |
| 35 | + |
| 36 | +To specify an ascending index key, use one of the ``ascending()`` methods. |
| 37 | + |
| 38 | +The following example specifies an ascending index key for the ``quantity`` field: |
| 39 | + |
| 40 | +.. code-block:: scala |
| 41 | + |
| 42 | + ascending("quantity") |
| 43 | + |
| 44 | +The following example specifies a compound index key composed of the ``quantity`` |
| 45 | +field sorted in ascending order and the ``totalAmount`` field sorted in |
| 46 | +ascending order: |
| 47 | + |
| 48 | +.. code-block:: scala |
| 49 | + |
| 50 | + ascending("quantity", "totalAmount") |
| 51 | + |
| 52 | +Descending |
| 53 | +---------- |
| 54 | + |
| 55 | +To specify a descending index key, use one of the ``descending()`` methods. |
| 56 | + |
| 57 | +The following example specifies a descending index key on the ``quantity`` field: |
| 58 | + |
| 59 | +.. code-block:: scala |
| 60 | + |
| 61 | + descending("quantity") |
| 62 | + |
| 63 | +The following example specifies a compound index key composed of the ``quantity`` |
| 64 | +field sorted in descending order and the ``totalAmount`` field sorted in |
| 65 | +descending order: |
| 66 | + |
| 67 | +.. code-block:: scala |
| 68 | + |
| 69 | + descending("quantity", "totalAmount") |
| 70 | + |
| 71 | +Compound Index |
| 72 | +-------------- |
| 73 | + |
| 74 | +To specify a compound index, use the ``compoundIndex()`` method. |
| 75 | + |
| 76 | +The following example specifies a compound index key composed of the ``quantity`` |
| 77 | +field sorted in ascending order, followed by the ``totalAmount`` field |
| 78 | +sorted in ascending order, followed by the ``orderDate`` field sorted in |
| 79 | +descending order: |
| 80 | + |
| 81 | +.. code-block:: scala |
| 82 | + |
| 83 | + compoundIndex(ascending("quantity", "totalAmount"), descending("orderDate")) |
| 84 | + |
| 85 | +Text Index |
| 86 | +---------- |
| 87 | + |
| 88 | +To specify a text index key, use the ``text()`` method. |
| 89 | + |
| 90 | +The following example specifies a text index key for the ``description`` field: |
| 91 | + |
| 92 | +.. code-block:: scala |
| 93 | + |
| 94 | + text("description") |
| 95 | + |
| 96 | +Hashed Index |
| 97 | +------------ |
| 98 | + |
| 99 | +To specify a hashed index key, use the ``hashed()`` method. |
| 100 | + |
| 101 | +The following example specifies a hashed index key for the ``timestamp`` field: |
| 102 | + |
| 103 | +.. code-block:: scala |
| 104 | + |
| 105 | + hashed("timestamp") |
| 106 | + |
| 107 | +Geospatial Index |
| 108 | +---------------- |
| 109 | + |
| 110 | +There are helpers for creating the index keys for the various geospatial |
| 111 | +indexes supported by MongoDB. |
| 112 | + |
| 113 | +2dsphere |
| 114 | +~~~~~~~~ |
| 115 | + |
| 116 | +To specify a 2dsphere index key, use one of the ``geo2dsphere()`` |
| 117 | +methods. |
| 118 | + |
| 119 | +The following example specifies a ``2dsphere`` index on the ``location`` field: |
| 120 | + |
| 121 | +.. code-block:: scala |
| 122 | + |
| 123 | + geo2dsphere("location") |
| 124 | + |
| 125 | +2d |
| 126 | +~~ |
| 127 | + |
| 128 | +To specify a ``2d`` index key, use the ``geo2d()`` method. |
| 129 | + |
| 130 | +.. important:: |
| 131 | + |
| 132 | + A ``2d`` index is for data stored as points on a two-dimensional plane |
| 133 | + and is intended for legacy coordinate pairs used in MongoDB Server |
| 134 | + version 2.2 and earlier. |
| 135 | + |
| 136 | +The following example specifies a ``2d`` index on the ``points`` field: |
| 137 | + |
| 138 | +.. code-block:: scala |
| 139 | + |
| 140 | + geo2d("points") |
0 commit comments