|
15 | 15 |
|
16 | 16 | using System.Linq.Expressions; |
17 | 17 | using MongoDB.Bson; |
18 | | -using MongoDB.Bson.Serialization.Serializers; |
| 18 | +using MongoDB.Bson.Serialization; |
| 19 | +using MongoDB.Bson.Serialization.Options; |
19 | 20 | using MongoDB.Driver.Linq.Linq3Implementation.Ast.Filters; |
20 | 21 | using MongoDB.Driver.Linq.Linq3Implementation.Misc; |
21 | 22 | using MongoDB.Driver.Linq.Linq3Implementation.Reflection; |
@@ -62,10 +63,36 @@ public static bool CanTranslate(Expression leftExpression, Expression rightExpre |
62 | 63 | public static AstFilter Translate(TranslationContext context, BinaryExpression expression, AstComparisonFilterOperator comparisonOperator, Expression enumerableExpression, Expression sizeExpression) |
63 | 64 | { |
64 | 65 | var fieldTranslation = ExpressionToFilterFieldTranslator.TranslateEnumerable(context, enumerableExpression); |
65 | | - SerializationHelper.EnsureRepresentationIsArray(enumerableExpression, fieldTranslation.Serializer); |
66 | 66 |
|
67 | | - if (TryConvertSizeExpressionToBsonValue(sizeExpression, out var size)) |
| 67 | + if (!TryConvertSizeExpressionToBsonValue(sizeExpression, out var size)) |
68 | 68 | { |
| 69 | + throw new ExpressionNotSupportedException(expression); |
| 70 | + } |
| 71 | + |
| 72 | + // Handle dictionary document representation for simple empty/not-empty checks |
| 73 | + if (fieldTranslation.Serializer is IBsonDictionarySerializer { DictionaryRepresentation: DictionaryRepresentation.Document }) |
| 74 | + { |
| 75 | + var sizeValue = size.ToInt64(); |
| 76 | + |
| 77 | + switch (comparisonOperator) |
| 78 | + { |
| 79 | + // Check for "not empty" patterns: Count > 0, Count >= 1, Count != 0 |
| 80 | + case AstComparisonFilterOperator.Gt when sizeValue == 0: |
| 81 | + case AstComparisonFilterOperator.Gte when sizeValue == 1: |
| 82 | + case AstComparisonFilterOperator.Ne when sizeValue == 0: |
| 83 | + return AstFilter.Ne(fieldTranslation.Ast, new BsonDocument()); |
| 84 | + |
| 85 | + // Check for "empty" patterns: Count == 0, Count <= 0, Count < 1 |
| 86 | + case AstComparisonFilterOperator.Eq when sizeValue == 0: |
| 87 | + case AstComparisonFilterOperator.Lte when sizeValue == 0: |
| 88 | + case AstComparisonFilterOperator.Lt when sizeValue == 1: |
| 89 | + return AstFilter.Eq(fieldTranslation.Ast, new BsonDocument()); |
| 90 | + } |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + SerializationHelper.EnsureRepresentationIsArray(enumerableExpression, fieldTranslation.Serializer); |
| 95 | + |
69 | 96 | switch (comparisonOperator) |
70 | 97 | { |
71 | 98 | case AstComparisonFilterOperator.Eq: |
|
0 commit comments