From 52192ad104b3228901f17b5c4579403d82c3a7fe Mon Sep 17 00:00:00 2001 From: Min Min Date: Fri, 1 Nov 2024 16:02:25 +0800 Subject: [PATCH] add index to label collection Signed-off-by: Min Min --- .../aslan/core/common/repository/mongodb/label.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/microservice/aslan/core/common/repository/mongodb/label.go b/pkg/microservice/aslan/core/common/repository/mongodb/label.go index 396d0117df..b8ba06b392 100644 --- a/pkg/microservice/aslan/core/common/repository/mongodb/label.go +++ b/pkg/microservice/aslan/core/common/repository/mongodb/label.go @@ -27,6 +27,7 @@ import ( "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" ) type LabelColl struct { @@ -47,8 +48,15 @@ func (c *LabelColl) GetCollectionName() string { } func (c *LabelColl) EnsureIndex(ctx context.Context) error { - // currently no query is required for the label defining collection - return nil + mod := mongo.IndexModel{ + Keys: bson.D{ + bson.E{Key: "key", Value: 1}, + }, + Options: options.Index().SetUnique(true), + } + + _, err := c.Indexes().CreateOne(ctx, mod) + return err } func (c *LabelColl) Create(args *models.Label) error {