Automatic MongoDB Index Creation from Go Struct Tags
mongoindexer is a lightweight and idiomatic Go package that automatically creates MongoDB indexes based on struct tags.
It’s designed to be generic, testable, and easy to integrate with any MongoDB project — no manual index management required.
- Automatically parses struct tags to generate indexes.
- Supports unique, ascending/descending, and TTL indexes.
- Simple, interface-based design for testing and extensibility.
- Fully covered with unit tests using
testify.
go get github.com/bhupenderlost/mongoindexerAdd the mongoIndex tag to specify index configuration.
package main
type User struct {
Email string `bson:"email" mongoIndex:"unique,asc,name=email_idx"`
CreatedAt int64 `bson:"created_at" mongoIndex:"ttl=3600"`
}package main
import (
"context"
"log"
"github.com/bhupenderlost/mongoindexer"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
ctx := context.Background()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
log.Fatal(err)
}
db := client.Database("testdb")
coll := db.Collection("users")
indexer := mongoindexer.New()
wrapped := mongoindexer.WrapCollection(coll)
if err := indexer.CreateIndexes(ctx, wrapped, User{}); err != nil {
log.Fatal("Index creation failed:", err)
}
log.Println("Indexes created successfully!")
}| Option | Description | Example |
|---|---|---|
asc / desc |
Index order (ascending / descending) | mongoIndex:"asc" |
unique |
Creates a unique index | mongoIndex:"unique,asc" |
ttl=<sec> |
Sets a TTL (Time-to-Live) index | mongoIndex:"ttl=3600" |
name=<idx> |
Custom index name | mongoIndex:"unique,asc,name=email" |
This package comes with a complete set of unit tests using testify and mock.
Run tests with:
go test ./...Example test:
func TestCreateIndexes_Success(t *testing.T) {
// mock IndexView, verify that CreateMany was called
}indexer/
│
├── indexer.go # Core logic for parsing and creating indexes
├── parser.go # Tag parsing and validation
├── interfaces.go # Mongo interfaces for abstraction
└── indexer_test.go # Unit tests using testify mocks for indexer
└── parser_test.go # Unit tests using testify mocks for parser
- Interfaces-first: Easily mockable for testing (
CollectionAPI,IndexView). - Generic: Works with any MongoDB collection that implements the minimal interface.
- Declarative: Indexes are defined once in the struct — no manual duplication.
When executed with the example User struct, the following indexes are created:
[
{
"key": { "email": 1 },
"name": "email_idx",
"unique": true
},
{
"key": { "created_at": 1 },
"expireAfterSeconds": 3600
}
]-
Fork the repository.
-
Create a feature branch:
git checkout -b feature/awesome-index
-
Commit your changes:
git commit -m "feat: add new index rule" -
Push and open a PR
Licensed under the MIT License.
See LICENSE for more information.