I am trying to create an api to get the data between two dates in NodeJS. My database is mongoDB, my data looks like this
_id:ObjectId("60ff19125474fa0ec45c2d1a")
username:"abc"
Date:"2021-7-26"
TimeIn:"2:02 PM"
TimeOut:"2:02 PM"
_id:ObjectId("6101f4a4676d8b2b743a2d58")
username:"abc"
Date:"2021-7-27"
TimeIn:"2:02 PM"
TimeOut:"2:02 PM"
_id:ObjectId("6102e1357ad1ed20d4642db0")
username:"abc"
Date:"2021-7-28"
TimeIn:"2:02 PM"
TimeOut:"2:02 PM"
_id:ObjectId("6104717cb025c025e8b2d15e")
username:"abc"
Date:"2021-8-01"
TimeIn:"2:02 PM"
TimeOut:"2:02 PM"
i want to fetch the data between two dates, i create an api to fetch data by date (single), need some modification to fetch it between two dates.
here is api to fetch data by date:
fetchHistorybydate =(req,res)=>{
var HistoryList = []
User.find({"username":req.body.username, "leave":{ $exists : false} },function(err,data){
for(var i =1 ;i<data.length;i++){
var NDate = data[i].Date;
if(NDate==req.body.date){
HistoryList.push(data[i])
}
}
if(err)
{res.status(300).json("Error")}
else{
var token = jwt.sign({
data: 'foobar'
}, 'secret', { expiresIn: "30 minute"})
res.status(200).json({AccessToken: token,data:HistoryList})
}
})
}
for example:
let say there are two calender on frontend one is for Fromdate
and second is for Todate
, if user select date from Fromdate
calender this 2021-7-26
and from Todate
calender this 2021-7-28
then it will only fetch the records from these dates.
the output should be, according to above data
_id:ObjectId("60ff19125474fa0ec45c2d1a")
username:"abc"
Date:"2021-7-26"
TimeIn:"2:02 PM"
TimeOut:"2:02 PM"
_id:ObjectId("6101f4a4676d8b2b743a2d58")
username:"abc"
Date:"2021-7-27"
TimeIn:"2:02 PM"
TimeOut:"2:02 PM"
_id:ObjectId("6102e1357ad1ed20d4642db0")
username:"abc"
Date:"2021-7-28"
TimeIn:"2:02 PM"
TimeOut:"2:02 PM"
Please help me how to do this.