44Indexes
55=======
66
7- .. default-domain:: mongodb
7+ .. facet::
8+ :name: genre
9+ :values: reference
10+
11+ .. meta::
12+ :keywords: code example
813
914.. contents:: On this page
1015 :local:
@@ -55,7 +60,7 @@ A query in MongoDB can contain the following elements:
5560
5661 * - Projection
5762 - **Optional**
58- - Specify the fields that MongoDB should return .
63+ - Specify the fields that MongoDB returns .
5964
6065 * - Sort
6166 - **Optional**
@@ -81,7 +86,7 @@ results directly from the index, also called a **covered query**.
8186 - ``name`` descending, ``age`` ascending
8287
8388 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.
8590
8691 To learn how to ensure your index covers your query criteria and
8792 projection, see :manual:`Query Coverage
@@ -91,14 +96,14 @@ Operational Considerations
9196~~~~~~~~~~~~~~~~~~~~~~~~~~
9297
9398To 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
96101each 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.
99104
100105Since 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
102107introduced :manual:`wildcard indexes </core/index-wildcard/>` to help
103108support these queries. Wildcard indexes are not designed to replace
104109workload-based index planning.
@@ -121,9 +126,9 @@ Single Field Indexes
121126~~~~~~~~~~~~~~~~~~~~
122127
123128Single field indexes holds a reference to a field within a
124- collection's documents.
129+ collection's documents.
125130
126- This index improves single field queries and sort performance, and
131+ This index improves single field queries and sort performance, and
127132supports TTL indexes that automatically remove documents from a
128133collection after a certain amount of time.
129134
@@ -145,21 +150,21 @@ The following example creates an index in ascending order on the
145150 .. input::
146151 :language: go
147152
153+ coll := client.Database("sample_mflix").Collection("movies")
148154 indexModel := mongo.IndexModel{
149- Keys: bson.D{{"title", 1}}
155+ Keys: bson.D{{"title", 1}},
150156 }
151157 name, err := coll.Indexes().CreateOne(context.TODO(), indexModel)
152158 if err != nil {
153159 panic(err)
154160 }
155-
156161 fmt.Println("Name of Index Created: " + name)
157162
158163 .. output::
159164 :language: none
160165 :visible: false
161166
162- title_1
167+ Name of Index Created: title_1
163168
164169.. _golang-compound-index:
165170
@@ -181,9 +186,10 @@ The following example creates a compound index on the ``fullplot`` and
181186 .. input::
182187 :language: go
183188
189+ coll := client.Database("sample_mflix").Collection("movies")
184190 indexModel := mongo.IndexModel{
185191 Keys: bson.D{
186- {"fullplot", -1},
192+ {"fullplot", -1},
187193 {"title", 1}
188194 }
189195 }
@@ -198,7 +204,7 @@ The following example creates a compound index on the ``fullplot`` and
198204 :language: none
199205 :visible: false
200206
201- fullplot_-1_title_1
207+ Name of Index Created: fullplot_-1_title_1
202208
203209Multikey Indexes (Indexes on Array Fields)
204210~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -220,6 +226,7 @@ field in the ``sample_mflix.movies`` collection:
220226 .. input::
221227 :language: go
222228
229+ coll := client.Database("sample_mflix").Collection("movies")
223230 indexModel := mongo.IndexModel{
224231 Keys: bson.D{{"cast", -1}}
225232 }
@@ -234,7 +241,7 @@ field in the ``sample_mflix.movies`` collection:
234241 :language: none
235242 :visible: false
236243
237- cast_-1
244+ Name of Index Created: cast_-1
238245
239246.. _golang-clustered-indexes:
240247
@@ -283,7 +290,7 @@ within the compound index.
283290
284291 Text indexes differ from the more powerful
285292 :atlas:`Atlas full text search indexes </atlas-search>`.
286- Atlas users should use Atlas search .
293+ We recommend Atlas search for Atlas users .
287294
288295Example
289296```````
@@ -297,7 +304,8 @@ collection:
297304
298305 .. input::
299306 :language: go
300-
307+
308+ coll := client.Database("sample_mflix").Collection("movies")
301309 indexModel := mongo.IndexModel{Keys: bson.D{{"title", 1}}}
302310 name, err := coll.Indexes().CreateOne(context.TODO(), indexModel)
303311 if err != nil {
@@ -310,7 +318,7 @@ collection:
310318 :language: none
311319 :visible: false
312320
313- plot_text
321+ Name of Index Created: plot_text
314322
315323.. _golang-geo-indexes:
316324
@@ -319,7 +327,7 @@ Geospatial Indexes
319327
320328MongoDB supports queries containing geospatial coordinate data by using
321329**2dsphere indexes**. A ``2dsphere`` index must be in a GeoJSON objects
322- field.
330+ field.
323331
324332This index allows you to perform the following:
325333
@@ -368,7 +376,7 @@ The following example creates a ``2dsphere`` index on the ``location.geo`` field
368376 :copyable: true
369377
370378 .. input::
371- :language: go
379+ :language: go
372380
373381 indexModel := mongo.IndexModel{
374382 Keys: bson.D{{"location.geo", "2dsphere"}}
@@ -393,7 +401,7 @@ Unique Indexes
393401
394402Unique indexes ensure that the indexed fields do not store duplicate
395403values. 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.
397405
398406To create a unique index, specify the field or combination of fields
399407that 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
411419 :language: go
412420
413421 indexModel := mongo.IndexModel{
414- Keys: bson.D{{"theaterId", -1}},
422+ Keys: bson.D{{"theaterId", -1}},
415423 Options: options.Index().SetUnique(true),
416424 }
417425 name, err := coll.Indexes().CreateOne(context.TODO(), indexModel)
@@ -425,7 +433,7 @@ The following example creates a unique, descending index on the ``theaterId`` fi
425433 :language: none
426434 :visible: false
427435
428- theaterId_-1
436+ Name of Index Created: theaterId_-1
429437
430438.. _golang-remove-index:
431439
@@ -445,6 +453,7 @@ in the ``sample_mflix.movies`` collection:
445453 .. input::
446454 :language: go
447455
456+ coll := client.Database("sample_mflix").Collection("movies")
448457 res, err := coll.Indexes().DropOne(context.TODO(), "title_1")
449458 if err != nil {
450459 panic(err)
0 commit comments