4
4
Indexes
5
5
=======
6
6
7
- .. default-domain:: mongodb
7
+ .. facet::
8
+ :name: genre
9
+ :values: reference
10
+
11
+ .. meta::
12
+ :keywords: code example
8
13
9
14
.. contents:: On this page
10
15
:local:
@@ -55,7 +60,7 @@ A query in MongoDB can contain the following elements:
55
60
56
61
* - Projection
57
62
- **Optional**
58
- - Specify the fields that MongoDB should return .
63
+ - Specify the fields that MongoDB returns .
59
64
60
65
* - Sort
61
66
- **Optional**
@@ -81,7 +86,7 @@ results directly from the index, also called a **covered query**.
81
86
- ``name`` descending, ``age`` ascending
82
87
83
88
Specifying a sort order of ``name`` and :guilabel:`age` ascending or :guilabel:`name` and ``age``
84
- descending would require an in-memory sort.
89
+ descending requires an in-memory sort.
85
90
86
91
To learn how to ensure your index covers your query criteria and
87
92
projection, see :manual:`Query Coverage
@@ -91,14 +96,14 @@ Operational Considerations
91
96
~~~~~~~~~~~~~~~~~~~~~~~~~~
92
97
93
98
To improve your query performance, create indexes on fields that appear
94
- often in your queries and operations that return sorted results. You
95
- should track index memory and disk usage for capacity planning since
99
+ often in your queries and operations that return sorted results. Track
100
+ index memory and disk usage for capacity planning since
96
101
each index that you add consumes disk space and memory. In addition,
97
- when a write operation updates an indexed field, MongoDB
98
- also has to update the related index.
102
+ when a write operation updates an indexed field, MongoDB
103
+ also must update the related index.
99
104
100
105
Since MongoDB supports dynamic schemas, your application can query
101
- against fields with currently unknown or arbitrary names. MongoDB 4.2
106
+ against fields with unknown or arbitrary names. MongoDB 4.2
102
107
introduced :manual:`wildcard indexes </core/index-wildcard/>` to help
103
108
support these queries. Wildcard indexes are not designed to replace
104
109
workload-based index planning.
@@ -121,10 +126,10 @@ Single Field Indexes
121
126
~~~~~~~~~~~~~~~~~~~~
122
127
123
128
Single field indexes holds a reference to a field within a
124
- collection's documents.
129
+ collection's documents.
125
130
126
- This index improves single field queries and sort performance, and
127
- supports TLS indexes that automatically remove documents from a
131
+ This index improves single field queries and sort performance, and
132
+ supports TTL indexes that automatically remove documents from a
128
133
collection after a certain amount of time.
129
134
130
135
.. note::
@@ -145,21 +150,21 @@ The following example creates an index in ascending order on the
145
150
.. input::
146
151
:language: go
147
152
153
+ coll := client.Database("sample_mflix").Collection("movies")
148
154
indexModel := mongo.IndexModel{
149
- Keys: bson.D{{"title", 1}}
155
+ Keys: bson.D{{"title", 1}},
150
156
}
151
157
name, err := coll.Indexes().CreateOne(context.TODO(), indexModel)
152
158
if err != nil {
153
159
panic(err)
154
160
}
155
-
156
161
fmt.Println("Name of Index Created: " + name)
157
162
158
163
.. output::
159
164
:language: none
160
165
:visible: false
161
166
162
- title_1
167
+ Name of Index Created: title_1
163
168
164
169
.. _golang-compound-index:
165
170
@@ -181,9 +186,10 @@ The following example creates a compound index on the ``fullplot`` and
181
186
.. input::
182
187
:language: go
183
188
189
+ coll := client.Database("sample_mflix").Collection("movies")
184
190
indexModel := mongo.IndexModel{
185
191
Keys: bson.D{
186
- {"fullplot", -1},
192
+ {"fullplot", -1},
187
193
{"title", 1}
188
194
}
189
195
}
@@ -198,7 +204,7 @@ The following example creates a compound index on the ``fullplot`` and
198
204
:language: none
199
205
:visible: false
200
206
201
- fullplot_-1_title_1
207
+ Name of Index Created: fullplot_-1_title_1
202
208
203
209
Multikey Indexes (Indexes on Array Fields)
204
210
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -220,6 +226,7 @@ field in the ``sample_mflix.movies`` collection:
220
226
.. input::
221
227
:language: go
222
228
229
+ coll := client.Database("sample_mflix").Collection("movies")
223
230
indexModel := mongo.IndexModel{
224
231
Keys: bson.D{{"cast", -1}}
225
232
}
@@ -234,7 +241,7 @@ field in the ``sample_mflix.movies`` collection:
234
241
:language: none
235
242
:visible: false
236
243
237
- cast_-1
244
+ Name of Index Created: cast_-1
238
245
239
246
.. _golang-clustered-indexes:
240
247
@@ -283,7 +290,7 @@ within the compound index.
283
290
284
291
Text indexes differ from the more powerful
285
292
:atlas:`Atlas full text search indexes </atlas-search>`.
286
- Atlas users should use Atlas search .
293
+ We recommend Atlas search for Atlas users .
287
294
288
295
Example
289
296
```````
@@ -297,7 +304,8 @@ collection:
297
304
298
305
.. input::
299
306
:language: go
300
-
307
+
308
+ coll := client.Database("sample_mflix").Collection("movies")
301
309
indexModel := mongo.IndexModel{Keys: bson.D{{"title", 1}}}
302
310
name, err := coll.Indexes().CreateOne(context.TODO(), indexModel)
303
311
if err != nil {
@@ -310,7 +318,7 @@ collection:
310
318
:language: none
311
319
:visible: false
312
320
313
- plot_text
321
+ Name of Index Created: plot_text
314
322
315
323
.. _golang-geo-indexes:
316
324
@@ -319,7 +327,7 @@ Geospatial Indexes
319
327
320
328
MongoDB supports queries containing geospatial coordinate data by using
321
329
**2dsphere indexes**. A ``2dsphere`` index must be in a GeoJSON objects
322
- field.
330
+ field.
323
331
324
332
This index allows you to perform the following:
325
333
@@ -368,7 +376,7 @@ The following example creates a ``2dsphere`` index on the ``location.geo`` field
368
376
:copyable: true
369
377
370
378
.. input::
371
- :language: go
379
+ :language: go
372
380
373
381
indexModel := mongo.IndexModel{
374
382
Keys: bson.D{{"location.geo", "2dsphere"}}
@@ -393,7 +401,7 @@ Unique Indexes
393
401
394
402
Unique indexes ensure that the indexed fields do not store duplicate
395
403
values. By default, MongoDB creates a unique index on the ``_id`` field
396
- during the creation of a collection.
404
+ during the creation of a collection.
397
405
398
406
To create a unique index, specify the field or combination of fields
399
407
that you want to prevent duplication on and set the ``unique`` option to
@@ -411,7 +419,7 @@ The following example creates a unique, descending index on the ``theaterId`` fi
411
419
:language: go
412
420
413
421
indexModel := mongo.IndexModel{
414
- Keys: bson.D{{"theaterId", -1}},
422
+ Keys: bson.D{{"theaterId", -1}},
415
423
Options: options.Index().SetUnique(true),
416
424
}
417
425
name, err := coll.Indexes().CreateOne(context.TODO(), indexModel)
@@ -425,7 +433,7 @@ The following example creates a unique, descending index on the ``theaterId`` fi
425
433
:language: none
426
434
:visible: false
427
435
428
- theaterId_-1
436
+ Name of Index Created: theaterId_-1
429
437
430
438
.. _golang-remove-index:
431
439
@@ -445,6 +453,7 @@ in the ``sample_mflix.movies`` collection:
445
453
.. input::
446
454
:language: go
447
455
456
+ coll := client.Database("sample_mflix").Collection("movies")
448
457
res, err := coll.Indexes().DropOne(context.TODO(), "title_1")
449
458
if err != nil {
450
459
panic(err)
0 commit comments