Comparing date field in Query does not work for me

query.greaterThan('createdAt', date)

where date is a native js Date object. So would be it greaterThan or lessThan - that part of query doesn’t affect anything like it absent. But because I use aggregation for my request as a solution I apply match in pipeline:

    const pipeline = [
        { match: { createdAt: { $gt: date } } },
        .....
      ];

But what can I do in simple query without aggregation?

I read something about intricacy with ISOdate and mongo but it did not clear anything. Tryed instead Date object substitute string (iso timestamp) and number (ms) - does not matter.

query.greaterThan('createdAt', date) should work properly. Do you mind to write a failing test case?

Thank for the answer. I replaced greaterThan to lessThan with no effect. If you say that it should work fine - my code had some simple/stupid mistake/mistyping.

I’m using date fields to compare values or ordering objects with no issues. Can you share an example code? Is date object defined?

I console.log date object (today) of course. Code look like this:

const now = new Date();
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());

const statQuery = await query()
  .equalTo('merchant', merchant);  // this condition working
  .greaterThan('createdAt', today); // this condition does not effect

const result = await statQuery.aggregate(pipeline)

Please take it easy, I understood for the future that date comparing work in simple query (and I workaround it already for that case via comparing inside aggregation), so this problem not so actual.

But I have more interesting question about group inside facet (will write it soon in new topic).

I believe you have to either use greaterThan or aggregate. They won’t work together in the same query.

Thank you, that’s the point. So with aggregation we must use for comparing only something inside aggregate, like match stage and comparing operators in my first message.

Yes. I believe they don’t work together. So, when using aggregate, you should use mongodb match operator instead of the sdk functions.