Skip to content

Commit 34f91e6

Browse files
craiggwilsonrstam
authored andcommitted
CSHARP-1661: support for $indexOfXXX.
1 parent 0c8e3b3 commit 34f91e6

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,46 @@ private bool TryTranslateStringMethodCall(MethodCallExpression node, out BsonVal
976976
}
977977
}
978978
break;
979+
case "IndexOf":
980+
var args = new BsonArray { field };
981+
if (node.Arguments[0].Type == typeof(char)
982+
&& node.Arguments[0].NodeType == ExpressionType.Constant)
983+
{
984+
args.Add(new BsonString(((ConstantExpression)node.Arguments[0]).Value.ToString()));
985+
}
986+
else
987+
{
988+
args.Add(TranslateValue(node.Arguments[0]));
989+
}
990+
991+
if (node.Arguments.Count > 1)
992+
{
993+
if (node.Arguments[1].Type != typeof(int))
994+
{
995+
throw new NotSupportedException("The StringComparison parameter is not supported in IndexOf.");
996+
}
997+
998+
args.Add(TranslateValue(node.Arguments[1]));
999+
}
1000+
1001+
if (node.Arguments.Count > 2)
1002+
{
1003+
if (node.Arguments[2].Type != typeof(int))
1004+
{
1005+
throw new NotSupportedException("The StringComparison parameter is not supported in IndexOf.");
1006+
}
1007+
1008+
var count = TranslateValue(node.Arguments[2]);
1009+
args.Add(new BsonDocument("$add", new BsonArray { args[2], count }));
1010+
}
1011+
1012+
if (node.Arguments.Count > 3)
1013+
{
1014+
throw new NotSupportedException("The StringComparison parameter is not supported in IndexOf.");
1015+
}
1016+
1017+
result = new BsonDocument("$indexOfBytes", args);
1018+
return true;
9791019
case "Split":
9801020
if (node.Arguments.Count < 1 || node.Arguments.Count > 2)
9811021
{

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,36 @@ public void Should_translate_hour()
548548
result.Value.Result.Should().Be(13);
549549
}
550550

551+
[SkippableFact]
552+
public void Should_translate_indexOf()
553+
{
554+
RequireServer.Where(minimumVersion: "3.3.6");
555+
556+
var result = Project(x => new { Result = x.A.IndexOf('e') });
557+
result.Projection.Should().Be("{ Result: { \"$indexOfBytes\": [\"$A\", \"e\"] }, _id: 0 }");
558+
result.Value.Result.Should().Be(2);
559+
560+
result = Project(x => new { Result = x.A.IndexOf("e") });
561+
result.Projection.Should().Be("{ Result: { \"$indexOfBytes\": [\"$A\", \"e\"] }, _id: 0 }");
562+
result.Value.Result.Should().Be(2);
563+
564+
result = Project(x => new { Result = x.A.IndexOf('e', 4) });
565+
result.Projection.Should().Be("{ Result: { \"$indexOfBytes\": [\"$A\", \"e\", 4] }, _id: 0 }");
566+
result.Value.Result.Should().Be(6);
567+
568+
result = Project(x => new { Result = x.A.IndexOf("e", 4) });
569+
result.Projection.Should().Be("{ Result: { \"$indexOfBytes\": [\"$A\", \"e\", 4] }, _id: 0 }");
570+
result.Value.Result.Should().Be(6);
571+
572+
result = Project(x => new { Result = x.A.IndexOf('e', 4, 2) });
573+
result.Projection.Should().Be("{ Result: { \"$indexOfBytes\": [\"$A\", \"e\", 4, { $add: [4, 2] }] }, _id: 0 }");
574+
result.Value.Result.Should().Be(-1);
575+
576+
result = Project(x => new { Result = x.A.IndexOf("e", 4, 2) });
577+
result.Projection.Should().Be("{ Result: { \"$indexOfBytes\": [\"$A\", \"e\", 4, { $add: [4, 2] }] }, _id: 0 }");
578+
result.Value.Result.Should().Be(-1);
579+
}
580+
551581
[Fact]
552582
public void Should_translate_less_than()
553583
{

0 commit comments

Comments
 (0)