0

I have an issue with Nestjs Mongoose about Date query I used Mongoose for my project. I want to query some documents based on Date field

My query looks like: this.model.find(startDate: {$gte: new Date().toUTCString()}) So it will convert to: 2023-05-17T09:37:23.592Z like this

And in my db. I have a record that has startEnd: 2023-05-17T12:30:23.306+00:00 I expected to get the doc but it didn't

But if I pasted above query to MongoDB Compass. It worked

When I console.log in pre hook find . It logged 2023-05-17T09:37:23.592Z But when I open Mongo debug mode. It shows: collection.find({ startDate: { '$lte': new Date("Wed, 17 May 2023 09:51:24 GMT") } }, { projection: null })

I don't know why it printed GMT here?

I use Window, Mongodb Docker with version 6.x

Please help me. Thanks in adavance

1 Answers1

0

Hi @Thai Nguyen firstly i saw difference between both queries, in first one you are using $gte but in second one you are using $lte.So, make sure which is correct one at the moment.

Coming to solution when you use .toUTCSTring() method it converts your date into string and in your db startDate might be stored in date type which is object.So you can remove this error by using simple date object collection.find({ startDate: { '$lte': new Date()} })