@@ -27,23 +27,29 @@ import (
27
27
"go.mongodb.org/mongo-driver/x/mongo/driver/session"
28
28
)
29
29
30
- // ErrInvalidIndexValue indicates that the index Keys document has a value that isn't either a number or a string.
30
+ // ErrInvalidIndexValue is returned if an index is created with a keys document that has a value that is not a number
31
+ // or string.
31
32
var ErrInvalidIndexValue = errors .New ("invalid index value" )
32
33
33
- // ErrNonStringIndexName indicates that the index name specified in the options is not a string.
34
+ // ErrNonStringIndexName is returned if an index is created with a name that is not a string.
34
35
var ErrNonStringIndexName = errors .New ("index name must be a string" )
35
36
36
- // ErrMultipleIndexDrop indicates that multiple indexes would be dropped from a call to IndexView.DropOne.
37
+ // ErrMultipleIndexDrop is returned if multiple indexes would be dropped from a call to IndexView.DropOne.
37
38
var ErrMultipleIndexDrop = errors .New ("multiple indexes would be dropped" )
38
39
39
- // IndexView is used to create, drop, and list indexes on a given collection.
40
+ // IndexView is a type that can be used to create, drop, and list indexes on a collection. An IndexView for a collection
41
+ // can be created by a call to Collection.Indexes().
40
42
type IndexView struct {
41
43
coll * Collection
42
44
}
43
45
44
- // IndexModel contains information about an index.
46
+ // IndexModel represents a new index to be created .
45
47
type IndexModel struct {
46
- Keys interface {}
48
+ // A document describing which keys should be used for the index. It cannot be nil. See
49
+ // https://docs.mongodb.com/manual/indexes/#indexes for examples of valid documents.
50
+ Keys interface {}
51
+
52
+ // The options to use to create the index.
47
53
Options * options.IndexOptions
48
54
}
49
55
@@ -54,7 +60,12 @@ func isNamespaceNotFoundError(err error) bool {
54
60
return false
55
61
}
56
62
57
- // List returns a cursor iterating over all the indexes in the collection.
63
+ // List executes a listIndexes command and returns a cursor over the indexes in the collection.
64
+ //
65
+ // The opts parameter can be used to specify options for this operation (see the options.ListIndexesOptions
66
+ // documentation).
67
+ //
68
+ // For more information about the command, see https://docs.mongodb.com/manual/reference/command/listIndexes/.
58
69
func (iv IndexView ) List (ctx context.Context , opts ... * options.ListIndexesOptions ) (* Cursor , error ) {
59
70
if ctx == nil {
60
71
ctx = context .Background ()
@@ -121,7 +132,8 @@ func (iv IndexView) List(ctx context.Context, opts ...*options.ListIndexesOption
121
132
return cursor , replaceErrors (err )
122
133
}
123
134
124
- // CreateOne creates a single index in the collection specified by the model.
135
+ // CreateOne executes a createIndexes command to create an index on the collection and returns the name of the new
136
+ // index. See the IndexView.CreateMany documentation for more information and an example.
125
137
func (iv IndexView ) CreateOne (ctx context.Context , model IndexModel , opts ... * options.CreateIndexesOptions ) (string , error ) {
126
138
names , err := iv .CreateMany (ctx , []IndexModel {model }, opts ... )
127
139
if err != nil {
@@ -131,8 +143,16 @@ func (iv IndexView) CreateOne(ctx context.Context, model IndexModel, opts ...*op
131
143
return names [0 ], nil
132
144
}
133
145
134
- // CreateMany creates multiple indexes in the collection specified by the models. The names of the
135
- // created indexes are returned.
146
+ // CreateMany executes a createIndexes command to create multiple indexes on the collection and returns the names of
147
+ // the new indexes.
148
+ //
149
+ // For each IndexModel in the models parameter, the index name can be specified via the Options field. If a name is not
150
+ // given, it will be generated from the Keys document.
151
+ //
152
+ // The opts parameter can be used to specify options for this operation (see the options.CreateIndexesOptions
153
+ // documentation).
154
+ //
155
+ // For more information about the command, see https://docs.mongodb.com/manual/reference/command/createIndexes/.
136
156
func (iv IndexView ) CreateMany (ctx context.Context , models []IndexModel , opts ... * options.CreateIndexesOptions ) ([]string , error ) {
137
157
names := make ([]string , 0 , len (models ))
138
158
@@ -354,7 +374,17 @@ func (iv IndexView) drop(ctx context.Context, name string, opts ...*options.Drop
354
374
return res , nil
355
375
}
356
376
357
- // DropOne drops the index with the given name from the collection.
377
+ // DropOne executes a dropIndexes operation to drop an index on the collection. If the operation succeeds, this returns
378
+ // a BSON document in the form {nIndexesWas: <int32>}. The "nIndexesWas" field in the response contains the number of
379
+ // indexes that existed prior to the drop.
380
+ //
381
+ // The name parameter should be the name of the index to drop. If the name is "*", ErrMultipleIndexDrop will be returned
382
+ // without running the command because doing so would drop all indexes.
383
+ //
384
+ // The opts parameter can be used to specify options for this operation (see the options.DropIndexesOptions
385
+ // documentation).
386
+ //
387
+ // For more information about the command, see https://docs.mongodb.com/manual/reference/command/dropIndexes/.
358
388
func (iv IndexView ) DropOne (ctx context.Context , name string , opts ... * options.DropIndexesOptions ) (bson.Raw , error ) {
359
389
if name == "*" {
360
390
return nil , ErrMultipleIndexDrop
@@ -363,7 +393,14 @@ func (iv IndexView) DropOne(ctx context.Context, name string, opts ...*options.D
363
393
return iv .drop (ctx , name , opts ... )
364
394
}
365
395
366
- // DropAll drops all indexes in the collection.
396
+ // DropAll executes a dropIndexes operation to drop all indexes on the collection. If the operation succeeds, this
397
+ // returns a BSON document in the form {nIndexesWas: <int32>}. The "nIndexesWas" field in the response contains the
398
+ // number of indexes that existed prior to the drop.
399
+ //
400
+ // The opts parameter can be used to specify options for this operation (see the options.DropIndexesOptions
401
+ // documentation).
402
+ //
403
+ // For more information about the command, see https://docs.mongodb.com/manual/reference/command/dropIndexes/.
367
404
func (iv IndexView ) DropAll (ctx context.Context , opts ... * options.DropIndexesOptions ) (bson.Raw , error ) {
368
405
return iv .drop (ctx , "*" , opts ... )
369
406
}
0 commit comments