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?
Hi Miquel, am I having the same problem as you? I’ve searched a lot on the internet but I haven’t found a solution. Did you manage to get around this problem? If yes, could you help me?
Not completely sure, but that is likely caused by the date not being a date object. If I remember correctly you have to send a Date object instance (see example below). If that is the case there is currently no way to parse the date from the Query box.
db.mycollection.find({
"active": true,
"createdAt" : {"$gte": new Date("2021-01-12T00:00.00Z")}
})
Thank you very much @RicardoE105, I am happy to be a part.
In fact your query is correct, but in the Query box when I instantiate the Date object, the query doesn’t work. I tried using JSON.stringify but it doesn’t work either because the new Date is processed and turns into a string.
For now I had to convert the field to timestamp and make the query comparing integers. But I would like to use it as a date, I have other situations where I won’t be able to get away from it.
Yes, as I mentioned, that is saldy not possible from the query box. We would need to abstract the filter functionality and provide an UI. Something where you can set the field, the type and the value.
We too are facing this issues, with no apaprent solutoin, thoug it is quite a bug limiation unfortuntely ! Has anyone got a workaround / solution since ?
Hey @ozanerturk, thanks so much for this workaround! I have been using the Function node for such queries until now, but your approach is much more elegant.