Last active
December 29, 2018 19:35
-
-
Save Hasstrup/4c2a13a27fe1859de938f657c63b0154 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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