-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Unknown
-
None
-
Affects Version/s: 2.30.0, 3.0.0, 3.14.1
-
Component/s: LINQ3, Performance
-
None
-
None
-
Dotnet Drivers
-
None
-
None
-
None
-
None
-
None
-
None
Context
We are migrating a .NET codebase to LINQ3 (MongoDB.Driver 2.30+). A very common pattern in our documents is a string Id serialized as ObjectId — the pattern recommended by the MongoDB .NET driver serialization documentation https://www.mongodb.com/docs/drivers/csharp/current/serialization/poco/#specify-an-id-generator
Our repositories use Expression<Func<T, bool>> filters such as x => x.Id == id. Functionally everything works: LINQ3 throws ExpressionNotSupportedException inside the regex translator, catches it in TryTranslateComparisonExpression, and falls back to the generic comparison path, which produces the correct { _id : ObjectId(...) } filter.
The problem is the throw/catch itself. It happens on every translation of every equality comparison on such a field, and each throw captures a stack trace. On hot paths this is measurable CPU/allocation overhead that is easy to avoid.
This is a follow-up to CSHARP-4803, which removed exceptions for flow control for string methods but left the equality comparison path throwing for fields that are not represented as strings.
Repro
net10.0, MongoDB.Driver 3.14.1 (latest published)
using System.Linq.Expressions; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Attributes; using MongoDB.Driver; using MongoDB.Driver.Linq; FirstChanceCounter.Start(); Console.WriteLine($"MongoDB.Driver : {typeof(MongoClient).Assembly.GetName().Version}"); Console.WriteLine($"Framework : {System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription}"); Console.WriteLine(); Console.WriteLine($"{"first-chance",12} | {"case",-48} | rendered filter"); Console.WriteLine(new string('-', 120)); var id = "650000000000000000000001"; var otherId = "650000000000000000000002"; var ids = new List<string> { id, otherId }; var serializer = BsonSerializer.SerializerRegistry.GetSerializer<Document>(); var cases = new (string Name, Expression<Func<Document, bool>> Expression)[] { ("x.Id == id", x => x.Id == id), ("x.Id != id", x => x.Id != id), ("x.Id == id && x.Name == \"n\"", x => x.Id == id && x.Name == "n"), ("x.Id == id || x.OtherId == id", x => x.Id == id || x.OtherId == id), ("x.Id == null", x => x.Id == null), ("x.Name == \"n\" (plain string)", x => x.Name == "n"), ("ids.Contains(x.Id)", x => ids.Contains(x.Id)) }; foreach (var (name, expression) in cases) { var before = FirstChanceCounter.Count; string rendered; try { rendered = new ExpressionFilterDefinition<Document>(expression) .Render(new RenderArgs<Document>(serializer, BsonSerializer.SerializerRegistry)) .ToJson(); } catch (Exception ex) { rendered = $"THREW {ex.GetType().Name}: {ex.Message}"; } Print(FirstChanceCounter.Count - before, name, rendered); } Console.WriteLine(); Console.WriteLine("first-chance = ExpressionNotSupportedException / FormatException thrown AND caught inside the LINQ3 translator."); static void Print(int firstChance, string name, string rendered) => Console.WriteLine($"{firstChance,12} | {name,-48} | {rendered}"); public sealed class Document { [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; } [BsonRepresentation(BsonType.ObjectId)] public string OtherId { get; set; } public string Name { get; set; } } public static class FirstChanceCounter { private static int _count; public static int Count => Volatile.Read(ref _count); public static void Start() => AppDomain.CurrentDomain.FirstChanceException += (_, args) => { if (args.Exception is ExpressionNotSupportedException or FormatException) { Interlocked.Increment(ref _count); } }; }
Output on 3.14.1:
MongoDB.Driver : 3.14.1.0
Framework : .NET 10.0.15
| first-chance | case | rendered filter |
|---|---|---|
| 1 | x.Id == id | { "_id" : \{ "$oid" : "650000000000000000000001" }
} |
| 1 | x.Id != id | { "_id" : \{ "$ne" : { "$oid" : "650000000000000000000001" } }
} |
| 1 | x.Id == id && x.Name == "n" | { "_id" : \{ "$oid" : "650000000000000000000001" }
, "Name" : "n" } |
| 2 | x.Id == id || x.OtherId == id | { "$or" : [\{ "_id" : { "$oid" : "650000000000000000000001" } }
, { "OtherId" : { "$oid" : "650000000000000000000001" }}] } |
| 1 | x.Id == null | { "_id" : null } |
| 0 | x.Name == "n" (plain string) | { "Name" : "n" } |
| 0 | ids.Contains(x.Id) | { "_id" : \{ "$in" : [{ "$oid" : "650000000000000000000001" }, \{ "$oid" : "650000000000000000000002" }
] } } |
Root cause
StringExpressionToRegexFilterTranslator private static bool IsStringEqualityComparison(Expression leftExpression, AstComparisonFilterOperator comparisonOperator) { return leftExpression.Type == typeof(string) && // only the CLR type is checked (comparisonOperator == AstComparisonFilterOperator.Eq || comparisonOperator == AstComparisonFilterOperator.Ne); } private static (AstFilterField, Modifiers) TranslateField(...) { // ... if (hasRepresentationSerializer.Representation != BsonType.String) { throw new ExpressionNotSupportedException( fieldExpression, expression, because: $"field \"{field.Path}\" is not represented as a string"); } // ... }
For an ObjectId-represented string the regex translator is always attempted, always throws, and TryTranslateComparisonExpression catches the exception and returns false — after which the generic comparison path produces the correct filter. The exception is used purely for flow control.
Proposed fix
diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/ExpressionTranslators/StringExpressionToRegexFilterTranslator.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/ExpressionTranslators/StringExpressionToRegexFilterTranslator.cs index ed4b943fd..72a31e046 100644 --- a/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/ExpressionTranslators/StringExpressionToRegexFilterTranslator.cs +++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/ExpressionTranslators/StringExpressionToRegexFilterTranslator.cs @@ -94,11 +94,11 @@ namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilter return false; } - public static bool CanTranslateComparisonExpression(Expression leftExpression, AstComparisonFilterOperator comparisonOperator, Expression rightExpression) + public static bool CanTranslateComparisonExpression(TranslationContext context, Expression leftExpression, AstComparisonFilterOperator comparisonOperator, Expression rightExpression) { return IsGetCharsComparison(leftExpression) || - IsStringEqualityComparison(leftExpression, comparisonOperator) || + (IsStringEqualityComparison(leftExpression, comparisonOperator) && CanUseRegexTranslator(context, leftExpression)) || IsStringIndexOfComparison(leftExpression) || IsStringLengthComparison(leftExpression) || IsStringCountComparison(leftExpression); } @@ -125,7 +125,7 @@ namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilter // caller is responsible for ensuring constant is on the right public static bool TryTranslateComparisonExpression(TranslationContext context, Expression expression, Expression leftExpression, AstComparisonFilterOperator comparisonOperator, Expression rightExpression, out AstFilter filter) { - if (CanTranslateComparisonExpression(leftExpression, comparisonOperator, rightExpression)) + if (CanTranslateComparisonExpression(context, leftExpression, comparisonOperator, rightExpression)) { try { @@ -271,6 +271,29 @@ namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilter (comparisonOperator == AstComparisonFilterOperator.Eq || comparisonOperator == AstComparisonFilterOperator.Ne); } + private static bool CanUseRegexTranslator(TranslationContext context, Expression leftExpression) + { + // String methods and modifiers (ToLower, ToUpper, Trim, ...) require the regex translator. + if (leftExpression is not MemberExpression) + { + return true; + } + + // A plain field comparison is translated identically by the generic comparison path. + // Only use the regex translator when the field is actually represented as a string; + // otherwise TranslateField throws ExpressionNotSupportedException and the caller falls + // back to the generic path anyway (using an exception for flow control). + try + { + var field = ExpressionToFilterFieldTranslator.Translate(context, leftExpression); + return field.Serializer is IHasRepresentationSerializer { Representation: BsonType.String }; + } + catch + { + return true; // preserve previous behavior + } + } + private static bool IsStringCountComparison(Expression leftExpression) { return
Why this is behavior-preserving
- CanUseRegexTranslator returns false only when the left operand is a plain field access (MemberExpression) whose serializer representation is not BsonType.String. In exactly that case TranslateField would throw ExpressionNotSupportedException, TryTranslateComparisonExpression would catch it, and the generic comparison path would run. Skipping the throwing attempt is therefore equivalent to the current behavior — the same generic path runs, just without the exception and the stack trace.
- Plain string fields still take the regex path: CanUseRegexTranslator resolves the field serializer, sees BsonType.String, and returns true. For a plain ==/!= without modifiers that path produces AstFilter.Eq(field, BsonString.Create(comparand)) (or AstFilter.Ne), which is identical to what the generic path would produce.
- String methods and modifiers (ToLower, ToUpper, Trim, StartsWith, Contains, EndsWith, IndexOf, Length, GetChars) are MethodCallExpression/UnaryExpression nodes, not member accesses, so CanUseRegexTranslator returns true before the serializer lookup and the regex path is unchanged.
Verified with a patched build:
3.14.1: 0 first-chance exceptions for all equality cases; rendered filters identical.
2.30.0: 0 first-chance exceptions; rendered filters identical.
x.Name == "n" and x.Name.ToLower() == "n" render exactly as before.
If a narrower change is preferred, the check can instead resolve the field serializer and skip the regex translator only when IHasRepresentationSerializer.Representation != BsonType.String, keeping the current path for plain string fields.
I've already verified the fix against 2.30.0 and 3.14.1 (no exceptions, identical filters); happy to open a PR with the change and regression tests if you'd like.
- related to
-
CSHARP-4066 Only use regex filters against string properties that are serialized as strings
-
- Closed
-
-
CSHARP-4809 If a string serializer doesn't implement IHasRepresentationSerializer don't assume string values are actually represented as strings
-
- Closed
-
-
CSHARP-4803 Refactor StringExpressionToRegexFilterTranslator.TryTranslate to not use an exception for flow control when possible
-
- Closed
-