$isNumber (aggregate) produces incorrect result when NaN is passed to it

MongoDB Web Shell

>> db.foo.find() 
[ { _id: } ]  
>> db.foo.aggregate({$project: {_id: , isNumber: {$isNumber: NaN} }})
[ { _id: , isNumber: true } ]

$isNumber must return false as NaN is not a number

Hello @Abhishek_Chaudhary1,

Welcome to the MongoDB Community forums :sparkles:

As per the NaN - MDN Web Docs,

The NaN (Not a Number) is a numeric data type that means an undefined value or value that cannot be represented, especially results of floating-point calculations.

Therefore, when using the $inNumber operator in MongoDB, it will return true for NaN.

test> db.collection.aggregate({$project: {_id: 1, isNumber: {$isNumber: NaN} }})
[ { _id: ObjectId("645919bea5f91f2bb30ec1ab"), isNumber: true } ]

Also, as per the $isNumeric - documentation, "decimal", "long", "int", "double" all leads to "isNumber": true, because they all are of numeric type.

Hope it clarifies your doubts. Feel free to reach out in case you have any further queries.

Best,
Kushagra

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.