There is a piece of code in my mongoDB / javascript setup that seems to be working intermittently. When comparing dates, the following code functions correctly:
let year = new Date().getFullYear();
User.aggregate([
{ $match: { emplyID: Number(req.params.emplyID) } },
{ $match: { "dateReq": { $gte: new Date(year, 0, 1), $lt: new Date(year+1, 0, 1) } } }
], function (err, pto) {
console.log(pto.length)
}
The console log shows 2 as the result.
On the contrary, this code snippet does not produce the expected output:
let year = new Date().getFullYear();
User.aggregate([
{ $match: { emplyID: Number(req.params.emplyID) } },
{ $match: { "dateReq": { $gte: new Date(year - 1, 0, 1), $lt:
new Date(year, 0, 1) } } }
], function (err, pto) {
console.log(pto.length)
}
For the second block, the console always displays 0.
Within the current database, we have the following entries:
DateReq : 3/9/19, DateReq : 3/8/19, DateReq : 10/20/18, DateReq: 11/15/18
The initial function aims to count the number of entries between 2018 and 2019, accomplishing it successfully with two matches. However, the second function intends to perform a similar count for data between 2017 and 2018, but even though the records are present, they cannot be identified. Any suggestions on what might be going wrong here?