Skip to content

Commit d761f9f

Browse files
author
Divjot Arora
authored
GODRIVER-872 IndexView documentation and examples (#232)
1 parent 4bdff2e commit d761f9f

File tree

2 files changed

+95
-12
lines changed

2 files changed

+95
-12
lines changed

mongo/crud_examples_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,3 +704,49 @@ func ExampleChangeStream_ResumeToken() {
704704
}
705705
defer newStream.Close(context.TODO())
706706
}
707+
708+
// IndexView examples
709+
710+
func ExampleIndexView_CreateMany() {
711+
var indexView *mongo.IndexView
712+
713+
// Create two indexes: {name: 1, email: 1} and {name: 1, age: 1}
714+
// For the first index, specify no options. The name will be generated as "name_1_email_1" by the driver.
715+
// For the second index, specify the Name option to explicitly set the name to "nameAge".
716+
models := []mongo.IndexModel{
717+
{
718+
Keys: bson.D{{"name", 1}, {"email", 1}},
719+
},
720+
{
721+
Keys: bson.D{{"name", 1}, {"age", 1}},
722+
Options: options.Index().SetName("nameAge"),
723+
},
724+
}
725+
726+
// Specify the MaxTime option to limit the amount of time the operation can run on the server
727+
opts := options.CreateIndexes().SetMaxTime(2 * time.Second)
728+
names, err := indexView.CreateMany(context.TODO(), models, opts)
729+
if err != nil {
730+
log.Fatal(err)
731+
}
732+
733+
fmt.Printf("created indexes %v\n", names)
734+
}
735+
736+
func ExampleIndexView_List() {
737+
var indexView *mongo.IndexView
738+
739+
// Specify the MaxTime option to limit the amount of time the operation can run on the server
740+
opts := options.ListIndexes().SetMaxTime(2 * time.Second)
741+
cursor, err := indexView.List(context.TODO(), opts)
742+
if err != nil {
743+
log.Fatal(err)
744+
}
745+
746+
// Get a slice of all indexes returned and print them out.
747+
var results []bson.M
748+
if err = cursor.All(context.TODO(), &results); err != nil {
749+
log.Fatal(err)
750+
}
751+
fmt.Println(results)
752+
}

mongo/index_view.go

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,29 @@ import (
2727
"go.mongodb.org/mongo-driver/x/mongo/driver/session"
2828
)
2929

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.
3132
var ErrInvalidIndexValue = errors.New("invalid index value")
3233

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.
3435
var ErrNonStringIndexName = errors.New("index name must be a string")
3536

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.
3738
var ErrMultipleIndexDrop = errors.New("multiple indexes would be dropped")
3839

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().
4042
type IndexView struct {
4143
coll *Collection
4244
}
4345

44-
// IndexModel contains information about an index.
46+
// IndexModel represents a new index to be created.
4547
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.
4753
Options *options.IndexOptions
4854
}
4955

@@ -54,7 +60,12 @@ func isNamespaceNotFoundError(err error) bool {
5460
return false
5561
}
5662

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/.
5869
func (iv IndexView) List(ctx context.Context, opts ...*options.ListIndexesOptions) (*Cursor, error) {
5970
if ctx == nil {
6071
ctx = context.Background()
@@ -121,7 +132,8 @@ func (iv IndexView) List(ctx context.Context, opts ...*options.ListIndexesOption
121132
return cursor, replaceErrors(err)
122133
}
123134

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.
125137
func (iv IndexView) CreateOne(ctx context.Context, model IndexModel, opts ...*options.CreateIndexesOptions) (string, error) {
126138
names, err := iv.CreateMany(ctx, []IndexModel{model}, opts...)
127139
if err != nil {
@@ -131,8 +143,16 @@ func (iv IndexView) CreateOne(ctx context.Context, model IndexModel, opts ...*op
131143
return names[0], nil
132144
}
133145

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/.
136156
func (iv IndexView) CreateMany(ctx context.Context, models []IndexModel, opts ...*options.CreateIndexesOptions) ([]string, error) {
137157
names := make([]string, 0, len(models))
138158

@@ -354,7 +374,17 @@ func (iv IndexView) drop(ctx context.Context, name string, opts ...*options.Drop
354374
return res, nil
355375
}
356376

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/.
358388
func (iv IndexView) DropOne(ctx context.Context, name string, opts ...*options.DropIndexesOptions) (bson.Raw, error) {
359389
if name == "*" {
360390
return nil, ErrMultipleIndexDrop
@@ -363,7 +393,14 @@ func (iv IndexView) DropOne(ctx context.Context, name string, opts ...*options.D
363393
return iv.drop(ctx, name, opts...)
364394
}
365395

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/.
367404
func (iv IndexView) DropAll(ctx context.Context, opts ...*options.DropIndexesOptions) (bson.Raw, error) {
368405
return iv.drop(ctx, "*", opts...)
369406
}

0 commit comments

Comments
 (0)