I can find some examples for creating indices:
- MongoDB index using Go - Christian Giacomi
- How to Create an Index Using the Golang Driver for MongoDB | ObjectRocket
But listing them is where things are falling down for me. I can’t seem to find an example of List being used online.
The mongo-go-driver docs just reference the underlying command being used:
// List executes a listIndexes command and returns a cursor over the indexes in the collection.
//
// The opts parameter can be used to specify options for this operation (see the options.ListIndexesOptions
// documentation).
//
// For more information about the command, see https://docs.mongodb.com/manual/reference/command/listIndexes/.
cursor, err := coll.Indexes().List(context.Background())
returns a cursor that I can call .All()
on and provide it an interface.
Unfortunately, unpacking into []mongo.IndexModel{}
doesn’t seem to work. @Divjot_Arora - would you happen to have a code snippet for listing indices?
Current code:
cursor, err := coll.Indexes().List(nil)
is.NoError(err)
indices := []mongo.IndexModel{}
is.NoError(cursor.All(nil, &indices))
is.GreaterOrEqual(len(indices), 9)