0

I am sending the startDate and endDate in the URL and hits the query to find the data between startDate and endDate

var startDate = res.req.query.startDate ? moment(res.req.query.startDate, 'YYYY-MM-DD').valueOf() : null
var endDate = res.req.query.endDate ? moment(res.req.query.endDate, 'YYYY-MM-DD').valueOf() : null
if (startDate && endDate) { 
   query.dispatchDate = { $gte:startDate , $lte: endDate }
}
Saurabh Narhe
  • 189
  • 1
  • 10
  • 1
    Possible duplicate of [Find objects between two dates MongoDB](https://stackoverflow.com/questions/2943222/find-objects-between-two-dates-mongodb) – Rafael Jan 22 '19 at 06:53

1 Answers1

0

You did not clearly asked your question but i am suggestion you a solution what i understand.

In order to find data between 2 dates first you must add a field in db to track when the record is entered let suppose you have a collection named items and field to track when data is enter is created_date then you can find data between 2 dates like

items.find({
     created_at: {
          $gte: ISODate("2019-01-21T00:00:00.000Z"),
          $lt: ISODate("2019-01-28T00:00:00.000Z")
     }
})

for more details shHow to find objects between 2 dates in mongodb

mzparacha
  • 627
  • 6
  • 20
  • dispatchDate is the field in the database on which I perform the query and strartDate and endDate is coming from frontend in the URL and I'm using npm moment to format the date – Saurabh kumar Jan 22 '19 at 06:47
  • You should really be saving dates as Date objects in mongodb not date strings. – Rafael Jan 22 '19 at 06:49
  • 1
    Please do not answer questions that already have answers. Instead, flag the question as a duplicate; [answer]. – Rafael Jan 22 '19 at 06:52
  • @Rafael Thanks for the suggestion i will follow that in future – mzparacha Jan 22 '19 at 10:37