I need to get data from my MongoDB using Nodejs API . I trying this
const attendanceRecords = await Attendence.find({
user: _id,
currentDate: { $gte: fromDate, $lte: toDate },
}).populate("user", "firstname lastname email");
But its not giving me correct data
currentDate field in my MongoDB database is date datatype and i am storing this date using this code
const mycurrentDate = new Date().toLocaleDateString('en-US', {
day: '2-digit',
month: '2-digit',
year: 'numeric',
});
const formattedDate = new Intl.DateTimeFormat("en-US", optionss).format(date);
the datatype of formattedDate is string Now how can I filter data and get data between a range of two dates ?
2
Answers
Assume using Mongoose with node, the query will be like that!
date_field_name your created_at field name in Collection
This error is likely caused by trying to store Date type as string type and filtering them as Date type during the query.