How would I write a query which checks the createdAt and finds all objects between 2022-05-22 and 2022-06-22? I tried the following code/ API, but it didn’t give back the right data :
getPoDetails = async (req, res, next) => {
try {
let fromDate = req.query.fromDate;
let toDate = req.query.toDate;
var findQry = {};
if(fromDate && toDate) {
findQry.createdAt = {$gte: new Date(fromDate), $lte: new Date(toDate)};
}
allmodels.poDetailsModel.find(findQry)
.then(data => res.status(200).send(data))
.catch(err => res.status(400).send(err));
} catch (error) {
next(error);
}
}
API link : localhost:8000/getPoDetails?fromDate=2022-05-22&toDate=2022-06-22