From bc137e9317efe7c4a9b03a022d10eebdb223b182 Mon Sep 17 00:00:00 2001 From: Toby Padilla Date: Mon, 5 May 2025 15:08:23 -0600 Subject: [PATCH] fix: ignore error on indexes already exist --- internal/database/mongo.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/database/mongo.go b/internal/database/mongo.go index 18cbeee..75afec1 100644 --- a/internal/database/mongo.go +++ b/internal/database/mongo.go @@ -3,6 +3,7 @@ package database import ( "context" "fmt" + "log" "time" "github.com/google/uuid" @@ -55,7 +56,12 @@ func NewMongoDB(ctx context.Context, connectionURI, databaseName, collectionName _, err = collection.Indexes().CreateMany(ctx, models) if err != nil { - return nil, err + // Mongo will error if the index already exists, we can ignore this and continue. + if err.(mongo.CommandError).Code != 86 { + return nil, err + } else { + log.Printf("Indexes already exists, skipping.") + } } return &MongoDB{