Mongo Node: prefiltering by dates

Hi,

I am trying to limit data returned in a query Find Operation by createdAt property greater than a defined date.

I know this is possible querying directly to Mongo client, but when I try to put this in a Find Operation in a users table:
{“active”: true, “createdAt” : { $gte : new Date(“2021-01-12T20:15:31Z”) }}

returns next error:
ERROR: Unexpected token $ in JSON at position 33

Is there any way to filter results by date with a $gte operator?

Thanks!

Try enclosing the $gte in quotes:

{"active": true, "createdAt" : { "$gte" : new Date("2021-01-12T20:15:31Z") }}
1 Like

Hi @mluhta,

I have added this query in a Find Operation and I get

ERROR: Unexpected token e in JSON at position 43

Any ideas?

What mongo version are you using? Trying to test this locally.

I am using next version: 4.2.8

Ok, you can use an expresion like this {{JSON.stringify({ "active": true, "createdAt": { $gte : "2021-01-12T20:15:31Z"} })}}

Thanks @RicardoE105.

I have checked and query is returning no data.
image

If I remove “createdAt” section, data is returned correctly.

Perhaps cuz there is not results that are active:true after that date?

Hi Ricardo,

There are results after date.
I will do some more tests.

Thank you!