Indexes¶
Goose allows you to define MongoDB indexes directly within your schema definition using AddIndex.
Defining Indexes¶
userSchema.AddIndex(mongo.IndexModel{
Keys: bson.D{{Key: "email", Value: 1}},
Options: options.Index().SetUnique(true),
})
userSchema.AddIndex(mongo.IndexModel{
Keys: bson.D{{Key: "username", Value: 1}, {Key: "status", Value: 1}},
})
When the model is compiled, Goose will automatically ensure these indexes exist in the MongoDB collection.
Index Creation in Production
By default, Goose creates indexes synchronously on startup. For large production collections, consider creating indexes manually in the background.