I want data between two dates how can I achieve this?

Hello everyone,

I am new to MongoDB and NodeJs I have been given with a task to get data in range of two dates, in below screenshot I have shared the image of function i am using, when i only try with one data the data is coming but when I try with two dates it is giving 0 entries can you please help

thanks in advance
Regards Juned,

Hello @Juned_Adenwalla, welcome to the MongoDB Community forum!

You can use the Query for Ranges when you want to get data between two dates. This is about querying data for a range of values.

In general, please do include a sample document being queried and also post the code used as formatted text (so that these can be used for trying your code and provide an answer).

Thank you for your reply, I have tried the code below but still i cant find the solution.
I want entry whos start data is greater than 22 feb and end date is less the 25 feb all the entries whos start and end data fall in between these should come

   ` var m = await model.find({ start_date : {$gte: req.params.sdate, $lt: req.params.edate}, resource: req.params.name, isdeleted: { $ne: true }})`
1 Like

Hello @Juned_Adenwalla,

You are trying to compare values from these two fields. (1) req.params.sdate, and (2) start_date. What are the values in these two fields?

In general, to compare two values, they must be of same data type. In MongoDB, the data types must be same when you want to compare them. Also:

Thank you for replying, I have to feilds in my data base start_date and end_date both are of same data type when users enters a start data and end data it should first verify if it a combination of two exists in database or not if exists return the number of listing with start_date and end_date in the given range,

I could have shared the sample document but i am afraid my company wont allow me as i am an intern sorry to bother you

1 Like

I have written this query I want any one of the condition to satisfy

            var m = await model.find({**start_date : {$gte: req.params.sdate, $lte: req.params.edate}** OR **end_date : {$lte: req.params.edate, $gte: req.params.sdate},** resource: req.params.name, isdeleted: { $ne: true }})

if anyone of the date is found in database return that entry

This query syntax looks fine to me. The req.params.sdate and req.params.edate are likely to have data as string type (as they are of req.params). So make sure that: (1) the MongoDB document field start_date is also of string type, and (2) that there is the data in the the req.params.

By using this query I am getting data but, if i select 23feb and 25feb again i get zero entries what i want is if any entry in start date is having between 22feb to 25fb should throw or if not found should go for end data having in same range