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.

user45232
  • 11
  • 2
  • 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 - http://stackoverflow.com/questions/2943222/find-objects-between-two-dates-mongodb – Vishwas Apr 28 '15 at 15:47

1 Answers1

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}})
}
harshad
  • 410
  • 4
  • 17
  • Thanks for the prompt reply. Is there any mongo query for the same? – user45232 Apr 28 '15 at 14:24
  • 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, – harshad Apr 30 '15 at 14:19