I'm using decimals to store my data on mongoDB, because I need to have as much precision as I can.
To use $sqrt on mongoDB you need to pass a double as argument, so I need to cast my decimal to double and then pass it to $sqrt.
Using mongoDB shell it's an easy job, but when you try to code this using mongoDB c# driver with IQueryable interface things getting complicated.
My code:
var data = _myRepository.AsQueryable()
.GetValues(equipmentId, startDate, endDate)
.GroupBy(g => g.timestamp)
.Select(p => new MyDTO {
timestamp = p.Key,
sqrt_calc = (decimal)Math.Sqrt(
(double)p.Sum(x => x.my_decimal_value)
)
.OrderBy(q => q.timestamp);
I'm getting the following exception: OverflowException: Arithmetic operation resulted in an overflow.
IQueryable is producing the query into query.txt attached.query.txt
As you can see there is a missing $toDouble after $sqrt, so $sqrt is receiving a decimal and produces a NaN which I believe that is causing the exception.
Is there a way to tell IQueryable to add the $toDouble cast?
- depends on
-
CSHARP-4397 Translate conversions to short form like $toInt instead of $convert when possible
- Closed