Hi,
I’m learning MongoDb, and get a little issue.
I’m trying to match data like this
$gte: new Date(`${year}-01-01`),
$lte: new Date(`${year}-12-31`),
but it’s returning me an empty array
when I’m trying such variant, it match the data which I needed
$gte: `${year}-01-01`,
$lte: `${year}-12-31`,
why such approach $gte: new Date(
${year}-01-01)
not working correctly and not match data?
this is a full function where I 'm using it
exports.getMonthlyPlan = async (req, res) => {
try {
const year = req.params.year * 1; // 2021
console.log(new Date(`${year}-01-01`));
const plan = await Tour.aggregate([
{
$unwind: '$startDates',
},
{
$match: {
startDates: {
$gte: new Date(`${year}-01-01`),
$lte: new Date(`${year}-12-31`),
// $gte: `${year}-01-01`,
// $lte: `${year}-12-31`,
},
},
},
]);
res.status(200).json({
status: 'success',
data: {
plan,
},
});
} catch (err) {
res.status(404).json({
status: 'fail',
message: err,
});
}
};