In MongoDB, I would like to use the comparison operators $ gt and $ lt, where the value may be null. When the operators did not work with a null value, I searched the documentation but could not find it. In both cases, he did not return any documents (although $ ne, $ gte and $ lte did indeed return documents, which means that there were documents equal and not equal to zero).
I would expect $ gt to function as $ ne (since the null type of Mongo comarison order is so low) and $ lt to return nothing for the same reason.
I was hoping this would work, as the value I pass to the request is a variable (potentially null), and I don't want to write a special case for null.
An example of what I studied, given the following collection:
{ id: 1, colNum: null } { id: 2, colNum: 72 } { id: 3 }
I expect the following request:
db.testtable.find( { "colNum" { $gt : null } } )
To return:
{ id: 2, colNum: 72 }
However, nothing was returned.
Is there a reason that $ gt and $ lt do not seem to work with zero, or is this a MongoDB error, or should it actually work, and is there probably a user error?
source share