Monday 18 June 2018

Mongoose query by date help

Good day people,I have a field with `{ type: Date }` and I want to query all dates created on a certain year and or month. I've currently written something like this:if (year && month && day) { return { $gte: new Date(year, month - 1, day, 0, 0, 0), $lte: new Date(year, month - 1, day, 23, 59, 59) } } if (year && month) { return { $gte: new Date(year, month - 1, 2, 0, 0, 0), $lte: new Date(year, month - 1, 28, 23, 59, 59) } } if (year) { return { $gte: new Date(year, 0, 2, 0, 0, 0), $lte: new Date(year, 11, 31, 23, 59, 59) } } However, it's not working. I have also tried an using `.aggregate` which works but I don't find it intuitive since I can't filter out by other fields after getting the aggregate result.Please help point me to the right direction, thanks!

Submitted June 18, 2018 at 10:59AM by chaseroyaleee

No comments:

Post a Comment