My goal is to find objects in my database where the difference between the date in the date field and the current date is greater than the value of the interval field.
My objects in the database look like this with a few more fields
[{
"date": {
"$date": "2022-02-04T12:00:00Z"
},
"interval": 5
},{
"date": {
"$date": "2022-02-04T12:00:00Z"
},
"interval": 60
}]
So if it would be 2022-02-04T12:30:00Z right now my query is supposed to return the first entry only, as only there the difference between the date and and the current date is greater than 5 minutes.
With a fix date in the query this is fairly easy:
{date: {$gte: ISODate('2022-02-04T12:30:00Z')}}.
With info from here, here and here I came up with this query:
db.articles.aggregate([{$project: {result: {$filter: {input: "$interval" as "interval", cond: {$gte : new Date(ISODate().getTime()-1000* interval)}}}}}])
Sadly it's not valid and throws a SyntaxError, but I don't know what to do. Since I have done very little with mongodb so far, I am getting nowhere.
How can I subtract the value of the interval field from the current date to use that in my query?