Skip to content

Commit b071749

Browse files
craiggwilsonrstam
authored andcommitted
CSHARP-1660: added support for both bytes and codepoint variant.
1 parent 01b378f commit b071749

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/MongoDB.Driver/Linq/Translators/AggregateLanguageTranslator.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,11 @@ private bool TryTranslateStringMemberAccess(MemberExpression node, out BsonValue
814814
switch (node.Member.Name)
815815
{
816816
case "Length":
817-
result = new BsonDocument("$strLenCP", field);
817+
var name = _stringTranslationMode == AggregateStringTranslationMode.CodePoints ?
818+
"$strLenCP" :
819+
"$strLenBytes";
820+
821+
result = new BsonDocument(name, field);
818822
return true;
819823
}
820824

@@ -1123,11 +1127,9 @@ private bool TryTranslateStringMethodCall(MethodCallExpression node, out BsonVal
11231127
case "Substring":
11241128
if (node.Arguments.Count == 2)
11251129
{
1126-
var name = "$substr";
1127-
if (_stringTranslationMode == AggregateStringTranslationMode.CodePoints)
1128-
{
1129-
name = "$substrCP";
1130-
}
1130+
var name = _stringTranslationMode == AggregateStringTranslationMode.CodePoints ?
1131+
"$substrCP" :
1132+
"$substr";
11311133
result = new BsonDocument(name, new BsonArray(new[]
11321134
{
11331135
field,

tests/MongoDB.Driver.Tests/Linq/Translators/AggregateProjectTranslatorTests.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,12 +1165,24 @@ public void Should_translate_select_with_an_anonymous_type_then_where_to_map_the
11651165
}
11661166

11671167
[SkippableFact]
1168-
public void Should_translate_string_length()
1168+
public void Should_translate_strLenBytes()
11691169
{
11701170
RequireServer.Where(minimumVersion: "3.3.4");
11711171

11721172
var result = Project(x => new { Result = x.A.Length });
11731173

1174+
result.Projection.Should().Be("{ Result: { \"$strLenBytes\": \"$A\" }, _id: 0 }");
1175+
1176+
result.Value.Result.Should().Be(7);
1177+
}
1178+
1179+
[SkippableFact]
1180+
public void Should_translate_strLenCP()
1181+
{
1182+
RequireServer.Where(minimumVersion: "3.3.4");
1183+
1184+
var result = Project(x => new { Result = x.A.Length }, __codePointTranslationOptions);
1185+
11741186
result.Projection.Should().Be("{ Result: { \"$strLenCP\": \"$A\" }, _id: 0 }");
11751187

11761188
result.Value.Result.Should().Be(7);

0 commit comments

Comments
 (0)