I would like to query a mongo collection with documents containing a field called last update time. Using javascript, I'd like to query all active devices and retrieve all documents that have a last update time of at least 2 hours. This is how I'm currently doing this:
const Devices = await Devices.find({Status : "Active"});
for(let i = 0; i < Devices.length; i++){
let device = Devices[i];
let today = new Date();
// Comparison is in hrs
if(today.getHours() - device.LastUpdateTime.getHours() > 2)){
// Additional Logic
}
This works, but there are a lot of active devices so I'd like to specify my Mongo find. Is there a way to add this add the LastUpdateTime comparison within the Devices.find
itself? LastUpdateTime is of type Date.
Any help would be appreciated.