In my mongoDB database, I am searching for datasets with expired date values. When I say expired, I mean that the timestamp of the last element in an array is older than a certain interval from the current timestamp (determined by a category).
Each dataset contains a field
structured like this:
{
"field" : [
{
"category" : 1,
"date" : ISODate("2019-03-01T12:00:00.464Z")
},
{
"category" : 1,
"date" : ISODate("2019-03-01T14:52:50.464Z")
}
]
}
The category specifies the time interval. For instance, 'category 1' represents 90 minutes and 'category 2' stands for 120 minutes.
My goal is to retrieve datasets where the date value has expired, meaning that the last element in the array is older than 90 minutes before the current timestamp.
An example query could be:
Content.find({ 'field.$.date': { $gt: new Date() } })
However, I face two challenges with this approach:
- How do I target the last element in the array?
- How can I incorporate the category time interval into the query?