0

I have been trying to filter all the objects which are created within a specific date range as well a specific time range. For example: Consider the following scenario if following are my filter's start date, end date, start time and end time. startDate: 23/01/2015 endDate: 25/01/2015 startTime: 10:00:00 endTime: 16:00:00

So I need to filter all the objects which are created between the startDate(23/01/2015) and endDate(25/01/2015). Also the creation time should be between startTime(10:00:00) and endTime(16:00:00). So an object which is created on 24/01/2015 but at 09:00:00 will not be filtered according to my use case.

Is there any mongo query to achieve the same where we can pass date and time separately. If not kindly suggest some workaround.

1
  • 1
    What format of date did you stored in mongo collection? can you provide your sample data? If you use ISO date then you this this might be helpful - stackoverflow.com/questions/2943222/… Commented Apr 28, 2015 at 15:47

1 Answer 1

0
for (var d = new Date(2015, 0, 23, 10, 00, 00); d <= new Date(2015, 0,25,16,00,00); d.setDate(d.getDate() + 1)) {
    var d1 = d.setHours(d.getHours()+6);
    db.coll.find({createdDate:{$gte:d, $lte:d1}})
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the prompt reply. Is there any mongo query for the same?
Yup. you can modify the same java script. just query db.coll.find({createdDate:{$and:[{$gte:ISODate("2015-01-23 10:00:00Z"), $lte:ISODate("2015-01-23 16:00:00Z")}, {$gte:ISODate("2015-01-24 10:00:00Z"), $lte:ISODate("2015-01-24 16:00:00Z")}, {$gte:ISODate("2015-01-25 10:00:00Z"), $lte:ISODate("2015-01-25 16:00:00Z")}]}). Date format may vary based on your structure,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.