Problem with using greaterThan and lessThan in Queries

Hi members!
In my use-case, I have a Class, and this class has some columns. Each time that I want to query from this class, I need to filter results between a range of numbers.
I have a column named yearFrom and another named yearTo with the type of Number.
These columns behave like a range of years (2015 to 2020 for example!).
So I need to filter these using greaterThan and lessThan if I’m right, based on docs.

And this is what my code looks like:

const yearAsNumber = 2020;
const myQuery = new Parse.Query("MyClass");
myQuery.greaterThan("yearFrom", yearAsNumber);
myQuery.lessThan("yearTo", yearAsNumber);

const results = await myQuery.first();

But it’s not working at all! If I remove greaterThan and lessThan, everything working right.
So I’m not sure what is the problem. I’m using it wrong, or it’s a miss understanding with the usage of these methods?

Your code syntax looks fine. But what do you mean when you say

Does query throws an error or returns null object or return wrong object?

Can you share your example MyClass object?

My guess is you replaced queries wrong.

Use this:

const yearAsNumber = 2020;
const myQuery = new Parse.Query("MyClass");
myQuery.greaterThan("yearTo", yearAsNumber);
myQuery.lessThan("yearFrom", yearAsNumber);

const results = await myQuery.first();

Thanks for the response BTW.
It returns a null but it shouldn’t because I’m sure that it matches (without these filters, I have the result).
Take a look at this image, some fields in my class.

The yearFrom contains the small number and the yearTo contains the big number.
So, the filters are right.
And BTW, I changed the type of columns to String to make sure it’s not a problem with the type. But the same result.

Thanks.

That’s why I changed to filters.

For example your Object A has
yearFrom:2015
yearTo:2025

If you want to get this object your yearTo filter must be bigger than 2020. Because 2025 is bigger than 2020.
And your yearFrom filter must be smaller than 2020. Because 2015 is smaller than 2020.

Thats why I changed query.

In your Original query you used

myQuery.greaterThan("yearFrom", yearAsNumber);
//Your filter must return yearFrom bigger than 2020. But our Object A is 2015.

//And

myQuery.lessThan("yearTo", yearAsNumber);
//And our year to is 2025. But your query says it must be smaller than 2020. 
//thats why it returns null.

So Can you please try my query and tell the results?

@uzaysan is correct.

Based from your example, your database has one row with yearFrom = 2015 and yearTo = 2020 and your value for yearAsNumber is 2020. Definitely, you would not get the result unless there’s an error on your console where you could post it on github page. The reason for that is because, 2020 > 2015 = true but 2020 < 2020 = false and true && false = false hence the result. You could change your query to use greaterThanOrEqualTo and lessThanOrEqualTo to get the result or if that is what you want to happen.

I think my example numbers not right :smile:
Based on the picture I sent from my DB, assume 1393 as input number(yearAsNumber).
And yearFrom equals to 1390 and yearTo equals to 1400.
Then, the math would be:
1390 < 1393 < 1400 => true. right?

1393 is bigger than 1390 and smaller than 1400.

To get the result, based from @uzaysan’s solution

const yearAsNumber = 1393;
const myQuery = new Parse.Query("MyClass");
// 1400 > 1393 = true
myQuery.greaterThan("yearTo", yearAsNumber);

// 1390 < 1393 = true
myQuery.lessThan("yearFrom", yearAsNumber);

const results = await myQuery.first();

This is how you would read it, the value of yearTo is greater than yearAsNumber which is 1400 and 1393 respectively, and the value of yearFrom is less than yearAsNumber which is 1390 and 1393 respectively.

You could experiment with MongoDB - Query Documents to get the feel of it.

Thanks, guys @uzaysan @JeromeDeLeon.
It solved!