Closed adrianleh closed 3 years ago
Hi @adrianleh
mgm
is a wrapper of mongo go driver, it supports all native mongo go driver features, so you can use indices here too.
Something like this:
func init() {
// Setup the mgm default config
err := mgm.SetDefaultConfig(nil, "mgm_lab", options.Client().ApplyURI("mongodb://root:12345@localhost:27017"))
// Create indices if they doesn't created already:
indexName := "unique_username"
unique := true
o := options.IndexOptions{
Name: &indexName,
Unique: &unique,
}
keys := []bson.D{bson.E{Key: "username", Value: 1}}
// Assume user is our model
_, err := mgm.Coll(&User{}).Indexes().CreateOne(context.Background(), mongo.IndexModel{
Keys: keys,
Options: o,
})
if err != nil {
panic("something went wrong!")
}
}
Is your feature request related to a problem? Please describe. We'd like to accelerate search by field queries.
Describe the solution you'd like The option to create custom indices in collections.
Describe alternatives you've considered Indices is natively supported by MongoDB and provides exactly what we need - the native mongo go driver also supports this (see
https://christiangiacomi.com/posts/mongodb-index-using-go/
for a code example)Additional context None