Mongoose query by date ranges with moment Mongoose query by date ranges with moment mongoose mongoose

Mongoose query by date ranges with moment


Neither of your queries guaranty returning entries from today or yesterday, if that's what you are trying to accomplish. Otherwise they work perfectly as written.

Your today query returns all records from the hour/minute/second when called, but yesterday, to the current hour/minute... tomorrow. That's why you get dates from yesterday. If you want only records from today, you'll need to set the $gt condition to the actual end of yesterday (or the beginning of today). You can use the moment().endOf('day') function for this.

You yesterday criteria work similarly, in that they search between the current hour/minute time two days ago and a day ago. You probably don't have any data in that range. You need to set your $gt and $it dates to the end of the day as well.