0

I have a MongoDB collection with a test field which is a float. For some records, test is equal to NaN. When I try and make a query in Laravel I cannot retrieve the latest record where test is not NaN.

    $myData = MyData::where('reference', '=', $reference)
        ->whereNotNull('test')
        ->where('test', '!=' , "NaN")
        ->orderBy('date','desc')
        ->limit(1)
        ->get();
  • ->whereNotNull('test') does not work because I have either a float or NaN so it's never null
  • ->where('test', '!=' , "NaN") does not work because the field is a float and "NaN" is a string
  • I cannot do ->where('test', '!=' , NaN) because it returns an error

How can I make a query that takes care of these NaN values ? If possible, I would like to avoid taking more records and adding a loop to find the correct one. There must be a more effective way to do what I need.

Here is an example of a record in Mongo

little side note: I am using jenssegers-mongodb Laravel package

{
    reference: 'AAAA-A1',
    date: ISODate('2022-10-06T10:28:00.000Z'),
    test: NaN,
    another_float: 9.82,
}
Thbwun
  • 323
  • 1
  • 3
  • 14

0 Answers0