@@ -11,3 +11,181 @@ Indexes Builders
1111 :class: singlecol
1212
1313.. _indexes-builders:
14+
15+ Overview
16+ --------
17+
18+ This guide shows you how to specify **indexes** using the Java Driver
19+ :doc:`builders </fundamentals/builders/>`. Index builders provide
20+ helper methods for the following types of indexes:
21+
22+ - :ref:`Ascending Indexes <ascending-indexes>`
23+ - :ref:`Descending Indexes <descending-indexes>`
24+ - :ref:`Compound Indexes <index-compound-indexes>`
25+ - :ref:`Text Indexes <index-text-indexes>`
26+ - :ref:`Hashed Indexes <index-hashed-indexes>`
27+ - :ref:`Geospatial Indexes <index-geospatial-indexes>`
28+
29+ Indexes store a subset of the collection’s data set. The index stores
30+ the value of a specific field or set of fields, ordered by the value of
31+ the field. See our guide on :doc:`Indexes </fundamentals/indexes>` for
32+ examples of queries covered by indexes.
33+
34+ The :java-core-api:`Indexes <com/mongodb/client/model/Indexes.html>`
35+ class provides static factory methods for all the MongoDB index types.
36+ Each method returns a :java-docs:`Bson <apidocs/bson/org/bson/conversions/Bson.html>`
37+ instance, which you can pass to
38+ :java-sync-api:`createIndex() <com/mongodb/client/MongoCollection.html#createIndex(org.bson.conversions.Bson)>`.
39+
40+ .. tip::
41+
42+ For brevity, you may choose to import all methods of the
43+ :java-core-api:`Indexes <com/mongodb/client/model/Indexes.html>`
44+ class statically:
45+
46+ .. code-block:: java
47+
48+ import static com.mongodb.client.model.Indexes.*;
49+
50+ The examples below assume this static import.
51+
52+ .. _ascending-indexes:
53+
54+ Ascending Indexes
55+ -----------------
56+
57+ An ascending index enables you to sort query results by the value of the
58+ indexed field(s) from smallest to largest.
59+
60+ In order to create an ascending index, first call the
61+ :java-core-api:`ascending() <com/mongodb/client/model/Indexes.html#ascending(java.lang.String...)>`
62+ builder method to create a ``Bson`` instance that represents the index
63+ document, passing the name or names of the fields you want to index.
64+ Then, call the ``createIndex()`` method on the collection, passing the ``Bson``
65+ instance that contains the index document.
66+
67+ .. note::
68+
69+ If you have an ascending or a descending index on a single field, MongoDB
70+ can sort using the index in either direction.
71+
72+ The following example specifies an ascending index on the ``name`` field:
73+
74+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Indexes.java
75+ :language: java
76+ :dedent:
77+ :start-after: begin ascendingIndex
78+ :end-before: end ascendingIndex
79+
80+ .. _descending-indexes:
81+
82+ Descending Indexes
83+ ------------------
84+
85+ A descending index enables you to sort query results by the value of the
86+ indexed field(s) from largest to smallest.
87+
88+ In order to create a descending index, first call the
89+ :java-core-api:`descending() <com/mongodb/client/model/Indexes.html#descending(java.lang.String...)>`
90+ builder method to create a ``Bson`` instance that represents the index
91+ document, passing the name or names of the fields you want to index.
92+ Then, call the ``createIndex()`` method on the collection, passing the ``Bson``
93+ instance that contains the index document.
94+
95+ The following example specifies a descending index on the ``capacity`` field:
96+
97+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Indexes.java
98+ :language: java
99+ :dedent:
100+ :start-after: begin descendingIndex
101+ :end-before: end descendingIndex
102+
103+ .. _index-compound-indexes:
104+
105+ Compound Indexes
106+ ----------------
107+
108+ In order to create a compound index, first call the
109+ :java-core-api:`compoundIndex() <com/mongodb/client/model/Indexes.html#compoundIndex(org.bson.conversions.Bson...)>`
110+ builder method to create a ``Bson`` instance that represents the index
111+ document, passing the names of the fields you want to index. Then, call
112+ the ``createIndex()`` method on the collection, passing the ``Bson``
113+ instance that contains the index document.
114+
115+ The following example specifies a compound index composed of
116+ descending index on the ``capacity`` and ``year`` field, followed
117+ by an ascending index on the ``name`` field:
118+
119+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Indexes.java
120+ :language: java
121+ :dedent:
122+ :start-after: begin compoundIndexExample
123+ :end-before: end compoundIndexExample
124+
125+ .. _index-text-indexes:
126+
127+ Text Indexes
128+ ------------
129+
130+ A text index groups documents by the text in the indexed field.
131+
132+ In order to create a text index, first call the
133+ :java-core-api:`text() <com/mongodb/client/model/Indexes.html#text(java.lang.String)>`
134+ builder method to create a ``Bson`` instance that represents the index
135+ document, passing the name of the fields you want to index. Then, call
136+ the ``createIndex()`` method on the collection, passing the ``Bson``
137+ instance that contains the index document.
138+
139+ The following example specifies a text index key on the "theaters" field:
140+
141+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Indexes.java
142+ :language: java
143+ :dedent:
144+ :start-after: begin textIndex
145+ :end-before: end textIndex
146+
147+ .. _index-hashed-indexes:
148+
149+ Hashed Indexes
150+ --------------
151+
152+ A hashed index groups documents by the hash value in the indexed field.
153+
154+ In order to create a hashed index, first call the
155+ :java-core-api:`hashed() <com/mongodb/client/model/Indexes.html#hashed(java.lang.String)>`
156+ builder method to create a ``Bson`` instance that represents the index
157+ document, passing the name of the fields you want to index. Then, call
158+ the ``createIndex()`` method on the collection, passing the ``Bson``
159+ instance that contains the index document.
160+
161+
162+ The following example specifies a hashed index on the ``capacity``
163+ field:
164+
165+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Indexes.java
166+ :language: java
167+ :dedent:
168+ :start-after: begin hashedIndex
169+ :end-before: end hashedIndex
170+
171+ .. _index-geospatial-indexes:
172+
173+ Geospatial Indexes
174+ ------------------
175+
176+ A geo2dsphere index groups documents by the coordinates in the indexed field.
177+
178+ In order to create a geo2dsphere index, first call the
179+ :java-core-api:`geo2dsphere() <com/mongodb/client/model/Indexes.html#geo2dsphere(java.lang.String...)>`
180+ builder method to create a ``Bson`` instance that represents the index
181+ document, passing the name or names of the fields you want to index.
182+ Then, call the ``createIndex()`` method on the collection, passing the ``Bson``
183+ instance that contains the index document.
184+
185+ The following example specifies a geo2dsphere index on the ``location`` field:
186+
187+ .. literalinclude:: /includes/fundamentals/code-snippets/builders/Indexes.java
188+ :language: java
189+ :dedent:
190+ :start-after: begin geo2dsphereIndex
191+ :end-before: end geo2dsphereIndex
0 commit comments