'MongoDB/Mongoose querying at current date?

Hi i'm trying to do a query to get results for one specific userId and it has to show for all future dates including now.date sorted from the most recent to the future.

Here is my collection:

 {
   "_id": "206d",
   "seatNumber": "76",
   "startDate": "2022-07-06T22:00:00.000Z",
   "userId": "34B24"
},
{
   "_id": "624a",
   "seatNumber": "12",
   "userId": "34B24",
   "startDate": "2021-07-06T22:00:00.000Z"
}


exports.getMyReservations = (req, res, next) => {
 Reservations.find( {userId: req.params.userId}).where('startDate':{$gt:Date.now()}).sort(asc)} 

But it's not working on postman. It supposed to show the first document result Please help



Solution 1:[1]

Try converting the date to ISO string with the help of javascript's toISOString() method

Reservations.find({userId: req.params.userId,
  "startDate": {$gt: new Date().toISOString()}
}).sort({})

query which I tried in Robo3t (GUI for MongoDB)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1