Skip to content

Commit 5f5e819

Browse files
committed
CSHARP-1402: added $sqrt expression support.
1 parent f7421ac commit 5f5e819

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Docs/reference/content/reference/driver/expressions.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ p => Math.Log(p.Age, 10);
622622
p => Math.Log10(p.Age);
623623
```
624624
```json
625-
{ $log: ["$Age"] }
625+
{ $log10: ["$Age"] }
626626
```
627627

628628
#### $mod
@@ -652,6 +652,15 @@ p => Math.Pow(p.Age, 10);
652652
{ $pow: ["$Age", 10] }
653653
```
654654

655+
#### $sqrt
656+
657+
```csharp
658+
p => Math.Sqrt(p.Age);
659+
```
660+
```json
661+
{ $sqrt: ["$Age"] }
662+
```
663+
655664
#### $subtract
656665

657666
```csharp

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,17 @@ public async Task Should_translate_set_union_reversed()
807807
result.Value.Result.Should().BeEquivalentTo("it", "icky", "not in here");
808808
}
809809

810+
[Test]
811+
[RequiresServer(MinimumVersion = "3.1.6")]
812+
public async Task Should_translate_sqrt()
813+
{
814+
var result = await Project(x => new { Result = Math.Sqrt(x.C.E.F) });
815+
816+
result.Projection.Should().Be("{ Result: { \"$sqrt\": [\"$C.E.F\"] }, _id: 0 }");
817+
818+
result.Value.Result.Should().BeApproximately(3.31662479, .0001);
819+
}
820+
810821
[Test]
811822
[RequiresServer(MinimumVersion = "3.1.6")]
812823
public async Task Should_translate_trunc()

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,12 @@ private bool TryTranslateStaticMathMethodCall(MethodCallExpression node, out Bso
736736
TranslateValue(node.Arguments[1])
737737
});
738738
return true;
739+
case "Sqrt":
740+
result = new BsonDocument("$sqrt", new BsonArray
741+
{
742+
TranslateValue(node.Arguments[0])
743+
});
744+
return true;
739745
case "Truncate":
740746
result = new BsonDocument("$trunc", TranslateValue(node.Arguments[0]));
741747
return true;

0 commit comments

Comments
 (0)