Skip to content

Instantly share code, notes, and snippets.

@Hasstrup
Last active December 29, 2018 19:35
Show Gist options
  • Save Hasstrup/4c2a13a27fe1859de938f657c63b0154 to your computer and use it in GitHub Desktop.
Save Hasstrup/4c2a13a27fe1859de938f657c63b0154 to your computer and use it in GitHub Desktop.
func PopulateIndex(database, collection string, client *mongo.Client) {
c := client.Database(database).Collection(collection)
opts := options.CreateIndexes().SetMaxTime(10 * time.Second)
index := yieldIndexModel()
c.Indexes().CreateOne(context.Background(), index, opts)
log.Println("Successfully create the index")
}
func yieldIndexModel() mongo.IndexModel {
keys := bsonx.Doc{{Key: *key, Value: bsonx.Int32(int32(*value))}}
index := mongo.IndexModel{}
index.Keys = keys
if *unique {
index.Options = bsonx.Doc{{Key: "unique", Value: bsonx.Boolean(true)}}
}
return index
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment