From 084cf418679aed29641afdd5b38cbb256837be29 Mon Sep 17 00:00:00 2001 From: Youngteac Hong Date: Wed, 11 Dec 2024 15:03:54 +0900 Subject: [PATCH] Simplify unique constraint for documents (#1098) This commit refined the project's document indexing strategy to accommodate unique key constraints while handling soft deletions. Previously, this was solved by applying partial index and unique constraint, but partial index could not be used when sharding. By adding the `removed_at` field into the uniqueness check, we now ensure only one active document exists at a time. --- server/backend/database/mongo/indexes.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/server/backend/database/mongo/indexes.go b/server/backend/database/mongo/indexes.go index 2af28b84a..d4d433cea 100644 --- a/server/backend/database/mongo/indexes.go +++ b/server/backend/database/mongo/indexes.go @@ -111,12 +111,9 @@ var collectionInfos = []collectionInfo{ Keys: bsonx.Doc{ {Key: "project_id", Value: bsonx.Int32(1)}, // shard key {Key: "key", Value: bsonx.Int32(1)}, + {Key: "removed_at", Value: bsonx.Int32(1)}, }, - Options: options.Index().SetPartialFilterExpression( - bsonx.Doc{ - {Key: "removed_at", Value: bsonx.Null()}, - }, - ).SetUnique(true), + Options: options.Index().SetUnique(true), }}, }, { name: ColChanges,