@@ -68,8 +68,8 @@ MongoDB supports a number of different index types to support querying
68
68
your data. The following sections describe the most common index types
69
69
and provide sample code for creating each index type.
70
70
71
- Single Field and Compound Indexes
72
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71
+ Single Field Indexes
72
+ ~~~~~~~~~~~~~~~~~~~~
73
73
74
74
**Single field indexes** are indexes that improve performance for queries
75
75
that specify ascending or descending sort order on a single field of a
@@ -83,6 +83,7 @@ the ``sample_mflix`` database.
83
83
:language: js
84
84
:start-after: begin-idx
85
85
:end-before: end-idx
86
+ :dedent:
86
87
87
88
The following is an example of a query that would be covered by the index
88
89
created above.
@@ -91,9 +92,12 @@ created above.
91
92
:language: js
92
93
:start-after: begin-query
93
94
:end-before: end-query
95
+ :dedent:
94
96
95
- See the MongoDB server manual section on
96
- :manual:`single field indexes </core/index-single>` for more information.
97
+ To learn more, see :manual:`Single Field Indexes </core/index-single>`.
98
+
99
+ Compound Indexes
100
+ ~~~~~~~~~~~~~~~~
97
101
98
102
**Compound indexes** are indexes that improve performance for queries that
99
103
specify ascending or descending sort order for *multiple* fields of
@@ -108,6 +112,7 @@ index on the ``type`` and ``genre`` fields in the ``movies`` collection in the
108
112
:language: js
109
113
:start-after: begin-idx
110
114
:end-before: end-idx
115
+ :dedent:
111
116
112
117
The following is an example of a query that would be covered by the index
113
118
created above.
@@ -116,9 +121,9 @@ created above.
116
121
:language: js
117
122
:start-after: begin-query
118
123
:end-before: end-query
124
+ :dedent:
119
125
120
- See the MongoDB server manual section on
121
- :manual:`Compound indexes </core/index-compound>` for more information.
126
+ To learn more, see :manual:`Compound Indexes </core/index-compound>`.
122
127
123
128
Multikey Indexes (Indexes on Array Fields)
124
129
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -136,6 +141,7 @@ the ``sample_mflix`` database.
136
141
:language: js
137
142
:start-after: begin-idx
138
143
:end-before: end-idx
144
+ :dedent:
139
145
140
146
The following is an example of a query that would be covered by the index
141
147
created above.
@@ -144,13 +150,44 @@ created above.
144
150
:language: js
145
151
:start-after: begin-query
146
152
:end-before: end-query
153
+ :dedent:
147
154
148
155
Multikey indexes behave differently from non-multikey indexes in terms of
149
156
query coverage, index bound computation, and sort behavior. For a full
150
157
explanation of multikey indexes, including a discussion of their behavior
151
158
and limitations, refer to the :manual:`Multikey Indexes page
152
159
</core/index-multikey>` in the MongoDB manual.
153
160
161
+ Clustered Indexes
162
+ ~~~~~~~~~~~~~~~~~
163
+
164
+ **Clustered indexes** are indexes that improve the performance of
165
+ insert, update, and delete operations on **clustered collections**.
166
+ Clustered collections store documents ordered by the clustered index key
167
+ value.
168
+
169
+ To create a clustered index, specify the ``clusteredIndex`` option in
170
+ the ``CollectionOption``. The ``clusteredIndex`` option must specify the
171
+ ``_id`` field as the key and the unique field as ``true``.
172
+
173
+ The following example uses the ``createCollection()`` method to create a
174
+ clustered index on the ``_id`` field in the ``vendors`` collection of the
175
+ ``tea`` database.
176
+
177
+ .. code-block:: javascript
178
+
179
+ const db = client.db('tea');
180
+ await db.createCollection('ratings', {
181
+ clusteredIndex: {
182
+ key: { _id: 1 },
183
+ unique: true
184
+ }
185
+ });
186
+
187
+ To learn more, see
188
+ :ref:`Clustered Indexes <db.createCollection.clusteredIndex>` and
189
+ :ref:`Clustered Collections <clustered-collections>`.
190
+
154
191
Text Indexes
155
192
~~~~~~~~~~~~
156
193
@@ -170,6 +207,7 @@ language.
170
207
:language: js
171
208
:start-after: begin-idx
172
209
:end-before: end-idx
210
+ :dedent:
173
211
174
212
The following is an example of a query that would be covered by the index
175
213
created above. Note that the ``sort`` is omitted because text indexes do not
@@ -179,9 +217,9 @@ contain sort order.
179
217
:language: js
180
218
:start-after: begin-query
181
219
:end-before: end-query
220
+ :dedent:
182
221
183
- For a full explanation of text search with MongoDB, refer to
184
- :manual:`Text Indexes </core/index-text>` in the MongoDB manual.
222
+ To learn more, see :manual:`Text Indexes </core/index-text>`.
185
223
186
224
Geospatial Indexes
187
225
~~~~~~~~~~~~~~~~~~
@@ -231,17 +269,17 @@ collection in the ``sample_mflix`` database to enable geospatial searches.
231
269
:language: js
232
270
:start-after: begin-idx
233
271
:end-before: end-idx
272
+ :dedent:
234
273
235
274
MongoDB also supports ``2d`` indexes for calculating distances on a
236
275
Euclidean plane and for working with the "legacy coordinate pairs" syntax
237
- used in MongoDB 2.2 and earlier. See the
238
- :manual:`Geospatial Queries page </geospatial-queries>` in the MongoDB
239
- server manual for more further information.
276
+ used in MongoDB 2.2 and earlier. To learn more, see
277
+ :manual:`Geospatial Queries </geospatial-queries>`.
240
278
241
279
Unique Indexes
242
280
~~~~~~~~~~~~~~
243
281
244
- Unique indexes ensure that the indexed fields do not store duplicate
282
+ ** Unique indexes** ensure that the indexed fields do not store duplicate
245
283
values. By default, MongoDB creates a unique index on the ``_id`` field
246
284
during the creation of a collection. To create a unique index, specify the
247
285
field or combination of fields that you want to prevent duplication on and
@@ -255,6 +293,7 @@ index on the ``theaterId`` field in the ``theaters`` collection of the
255
293
:language: js
256
294
:start-after: begin-idx
257
295
:end-before: end-idx
296
+ :dedent:
258
297
259
298
If you attempt to perform a write operation that stores a duplicate value
260
299
that violates the unique index, MongoDB will throw an error that resembles
@@ -265,5 +304,4 @@ the following:
265
304
266
305
E11000 duplicate key error index
267
306
268
- Refer to the :manual:`Unique Indexes page </core/index-unique>` in the
269
- MongoDB server manual for more information.
307
+ To learn more, see :manual:`Unique Indexes </core/index-unique>`.
0 commit comments