I have this date given by a date picker widget:
let targetDate = '2019-01-12';
All my documents have a createtAt date generated by timestamps:
"createdAt": "2019-01-12T21:49:05.546Z"
I want to get all documents that field createdAt matches with my given date. I tried using $elemMatch:
const allDailies = await Daily.find({ createdAt: { $elemMatch: dateTarget } });
const allDailies = await Daily.find({ createdAt: { $elemMatch: { from: dateTarget, to: dateTarget} } });
And no one works, how can I do this query? Actually I can't modify my Schema :(
EDIT:
router.get('/daily/:date', async (req, res) => {
try {
const targetDate = new RegExp(`^${req.params.date}`);
const allDailies = await Daily.find({ createdAt: { $regex: targetDate } });
console.log('Dailies', allDailies);
return res.json({ allDailies });
} catch (error) {
return res.sendStatus(httpStatus.INTERNAL_SERVER_ERROR);
}
});
It returns a 500 error