Skip to content

Commit d04885e

Browse files
craiggwilsonrstam
authored andcommitted
CSHARP-1661: added bytes and cp variants.
1 parent b071749 commit d04885e

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,11 @@ private bool TryTranslateStringMethodCall(MethodCallExpression node, out BsonVal
10841084
indexOfArgs.Add(endIndex);
10851085
}
10861086

1087-
result = new BsonDocument("$indexOfBytes", indexOfArgs);
1087+
var indexOpName = _stringTranslationMode == AggregateStringTranslationMode.CodePoints ?
1088+
"$indexOfCP" :
1089+
"$indexOfBytes";
1090+
1091+
result = new BsonDocument(indexOpName, indexOfArgs);
10881092
return true;
10891093
case "Split":
10901094
if (node.Arguments.Count < 1 || node.Arguments.Count > 2)
@@ -1127,10 +1131,10 @@ private bool TryTranslateStringMethodCall(MethodCallExpression node, out BsonVal
11271131
case "Substring":
11281132
if (node.Arguments.Count == 2)
11291133
{
1130-
var name = _stringTranslationMode == AggregateStringTranslationMode.CodePoints ?
1134+
var substrOpName = _stringTranslationMode == AggregateStringTranslationMode.CodePoints ?
11311135
"$substrCP" :
11321136
"$substr";
1133-
result = new BsonDocument(name, new BsonArray(new[]
1137+
result = new BsonDocument(substrOpName, new BsonArray(new[]
11341138
{
11351139
field,
11361140
TranslateValue(node.Arguments[0]),

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ public void Should_translate_hour()
554554
}
555555

556556
[SkippableFact]
557-
public void Should_translate_indexOf()
557+
public void Should_translate_indexOfBytes()
558558
{
559559
RequireServer.Where(minimumVersion: "3.3.6");
560560

@@ -583,6 +583,36 @@ public void Should_translate_indexOf()
583583
result.Value.Result.Should().Be(-1);
584584
}
585585

586+
[SkippableFact]
587+
public void Should_translate_indexOfCP()
588+
{
589+
RequireServer.Where(minimumVersion: "3.3.6");
590+
591+
var result = Project(x => new { Result = x.A.IndexOf('e') }, __codePointTranslationOptions);
592+
result.Projection.Should().Be("{ Result: { \"$indexOfCP\": [\"$A\", \"e\"] }, _id: 0 }");
593+
result.Value.Result.Should().Be(2);
594+
595+
result = Project(x => new { Result = x.A.IndexOf("e") }, __codePointTranslationOptions);
596+
result.Projection.Should().Be("{ Result: { \"$indexOfCP\": [\"$A\", \"e\"] }, _id: 0 }");
597+
result.Value.Result.Should().Be(2);
598+
599+
result = Project(x => new { Result = x.A.IndexOf('e', 4) }, __codePointTranslationOptions);
600+
result.Projection.Should().Be("{ Result: { \"$indexOfCP\": [\"$A\", \"e\", 4] }, _id: 0 }");
601+
result.Value.Result.Should().Be(6);
602+
603+
result = Project(x => new { Result = x.A.IndexOf("e", 4) }, __codePointTranslationOptions);
604+
result.Projection.Should().Be("{ Result: { \"$indexOfCP\": [\"$A\", \"e\", 4] }, _id: 0 }");
605+
result.Value.Result.Should().Be(6);
606+
607+
result = Project(x => new { Result = x.A.IndexOf('e', 4, 2) }, __codePointTranslationOptions);
608+
result.Projection.Should().Be("{ Result: { \"$indexOfCP\": [\"$A\", \"e\", 4, { $add: [4, 2] }] }, _id: 0 }");
609+
result.Value.Result.Should().Be(-1);
610+
611+
result = Project(x => new { Result = x.A.IndexOf("e", 4, 2) }, __codePointTranslationOptions);
612+
result.Projection.Should().Be("{ Result: { \"$indexOfCP\": [\"$A\", \"e\", 4, { $add: [4, 2] }] }, _id: 0 }");
613+
result.Value.Result.Should().Be(-1);
614+
}
615+
586616
[Fact]
587617
public void Should_translate_less_than()
588618
{

0 commit comments

Comments
 (0)