From 361847cae26d970de322e7f398ec27fa8bdc5db8 Mon Sep 17 00:00:00 2001 From: hz Date: Mon, 13 Jul 2026 10:25:44 -0500 Subject: [PATCH] Handle legacy attempts during statistics backfill --- .../Services/AnswerStatisticsStore.cs | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/api/QuestionsApi/Services/AnswerStatisticsStore.cs b/api/QuestionsApi/Services/AnswerStatisticsStore.cs index 9167d02..e51035f 100644 --- a/api/QuestionsApi/Services/AnswerStatisticsStore.cs +++ b/api/QuestionsApi/Services/AnswerStatisticsStore.cs @@ -154,6 +154,10 @@ public sealed class AnswerStatisticsStore { return false; } + catch (MongoCommandException exception) when (exception.Code == 11000) + { + return false; + } } public async Task MarkBackfillCompleteAsync(string owner) @@ -203,7 +207,13 @@ public sealed class AnswerStatisticsStore new BsonDocument("$match", match), new BsonDocument("$group", new BsonDocument { - { "_id", new BsonDocument { { "q", "$QuestionExternalId" }, { "b", "$CounterBucket" }, { "c", "$ChoiceId" } } }, + { "_id", new BsonDocument + { + { "q", "$QuestionExternalId" }, + { "b", new BsonDocument("$ifNull", new BsonArray { "$CounterBucket", 0 }) }, + { "c", "$ChoiceId" } + } + }, { "count", new BsonDocument("$sum", 1) } }) }; @@ -214,7 +224,13 @@ public sealed class AnswerStatisticsStore new BsonDocument("$unwind", "$PresentedChoiceIds"), new BsonDocument("$group", new BsonDocument { - { "_id", new BsonDocument { { "q", "$QuestionExternalId" }, { "b", "$CounterBucket" }, { "c", "$PresentedChoiceIds" } } }, + { "_id", new BsonDocument + { + { "q", "$QuestionExternalId" }, + { "b", new BsonDocument("$ifNull", new BsonArray { "$CounterBucket", 0 }) }, + { "c", "$PresentedChoiceIds" } + } + }, { "count", new BsonDocument("$sum", 1) } }) }; @@ -231,7 +247,12 @@ public sealed class AnswerStatisticsStore new BsonDocument("$match", legacyMatch), new BsonDocument("$group", new BsonDocument { - { "_id", new BsonDocument { { "q", "$QuestionExternalId" }, { "b", "$CounterBucket" } } }, + { "_id", new BsonDocument + { + { "q", "$QuestionExternalId" }, + { "b", new BsonDocument("$ifNull", new BsonArray { "$CounterBucket", 0 }) } + } + }, { "count", new BsonDocument("$sum", 1) } }) };