Skip to content

Commit 559749c

Browse files
craiggwilsonrstam
authored andcommitted
CSHARP-1650: added support for $split.
1 parent 79853e2 commit 559749c

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,57 @@ private bool TryTranslateStringMethodCall(MethodCallExpression node, out BsonVal
976976
}
977977
}
978978
break;
979+
case "Split":
980+
BsonValue separator = null;
981+
var separatorConstant = node.Arguments[0] as ConstantExpression;
982+
if (separatorConstant != null && separatorConstant.Type == typeof(char[]))
983+
{
984+
var chars = (char[])separatorConstant.Value;
985+
if (chars.Length == 1)
986+
{
987+
separator = new BsonString(new string(chars));
988+
}
989+
}
990+
else
991+
{
992+
separator = TranslateValue(node.Arguments[0]);
993+
var array = separator as BsonArray;
994+
if (array != null && array.Count == 1)
995+
{
996+
separator = array[0];
997+
}
998+
else if (array != null)
999+
{
1000+
separator = null;
1001+
}
1002+
}
1003+
1004+
if (separator != null)
1005+
{
1006+
if (node.Arguments.Count == 1)
1007+
{
1008+
result = new BsonDocument("$split", new BsonArray
1009+
{
1010+
field,
1011+
separator
1012+
});
1013+
return true;
1014+
}
1015+
else if (node.Arguments.Count == 2 && node.Arguments[1].Type == typeof(StringSplitOptions))
1016+
{
1017+
var splitOptions = node.Arguments[1] as ConstantExpression;
1018+
if (splitOptions != null && ((StringSplitOptions)splitOptions.Value) == StringSplitOptions.None)
1019+
{
1020+
result = new BsonDocument("$split", new BsonArray
1021+
{
1022+
field,
1023+
separator
1024+
});
1025+
return true;
1026+
}
1027+
}
1028+
}
1029+
break;
9791030
case "Substring":
9801031
if (node.Arguments.Count == 2)
9811032
{

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,28 @@ public void Should_translate_string_length()
11171117
result.Value.Result.Should().Be(7);
11181118
}
11191119

1120+
[SkippableFact]
1121+
public void Should_translate_split()
1122+
{
1123+
RequireServer.Where(minimumVersion: "3.3.6");
1124+
1125+
var result = Project(x => new { Result = x.A.Split('e') });
1126+
result.Projection.Should().Be("{ Result: { \"$split\": [\"$A\", \"e\" ] }, _id: 0 }");
1127+
result.Value.Result.Should().BeEquivalentTo("Aw", "som", "");
1128+
1129+
result = Project(x => new { Result = x.A.Split(new[] { 'e' }) });
1130+
result.Projection.Should().Be("{ Result: { \"$split\": [\"$A\", \"e\" ] }, _id: 0 }");
1131+
result.Value.Result.Should().BeEquivalentTo("Aw", "som", "");
1132+
1133+
result = Project(x => new { Result = x.A.Split(new[] { 'e' }, StringSplitOptions.None) });
1134+
result.Projection.Should().Be("{ Result: { \"$split\": [\"$A\", \"e\" ] }, _id: 0 }");
1135+
result.Value.Result.Should().BeEquivalentTo("Aw", "som", "");
1136+
1137+
result = Project(x => new { Result = x.A.Split(new[] { "es" }, StringSplitOptions.None) });
1138+
result.Projection.Should().Be("{ Result: { \"$split\": [\"$A\", \"es\" ] }, _id: 0 }");
1139+
result.Value.Result.Should().BeEquivalentTo("Aw", "ome");
1140+
}
1141+
11201142
[SkippableFact]
11211143
public void Should_translate_stdDevPop()
11221144
{

0 commit comments

Comments
 (0)