Skip to content

Commit fe522a9

Browse files
authored
DOCSP-38764: indexes builders (#32)
1 parent 3a008e9 commit fe522a9

File tree

2 files changed

+142
-4
lines changed

2 files changed

+142
-4
lines changed

source/builders.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Builders
1111
/builders/sorts/
1212
/builders/aggregates/
1313
/builders/updates/
14+
/builders/indexes/
1415

1516
The {+driver-short+} provides the following classes that make it easier to use
1617
the CRUD API:
@@ -20,10 +21,7 @@ the CRUD API:
2021
- :ref:`scala-builders-sorts`: Support for building sort criteria
2122
- :ref:`scala-builders-agg`: Support for building aggregation pipelines
2223
- :ref:`scala-builders-updates`: Support for building updates
23-
24-
.. TODO replace with links
25-
26-
- Indexes: Support for creating index keys
24+
- :ref:`scala-builders-indexes`: Support for creating index keys
2725

2826
.. important::
2927

source/builders/indexes.txt

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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

Comments
 (0)