title: MongoDB - Querying between a time range of hours
body: <p>I have a MongoDB datastore set up with location data stored like this:</p>
  
  <pre><code>{
  "_id" : ObjectId("51d3e161ce87bb000792dc8d"),
  "datetime_recorded" : ISODate("2013-07-03T05:35:13Z"),
  "loc" : {
      "coordinates" : [
          0.297716,
          18.050614
      ],
      "type" : "Point"
  },
  "vid" : "11111-22222-33333-44444"
  }
  </code></pre>
  
  <p>I'd like to be able to perform a query similar to the <a href="http://cookbook.mongodb.org/patterns/date_range/" rel="noreferrer">date range</a> example but instead on a <em>time range</em>. i.e. Retrieve all points recorded between 12AM and 4PM (can be done with 1200 and 1600 24 hour time as well).</p>
  
  <p>e.g. </p>
  
  <p>With points:</p>
  
  <ul>
  <li><code>"datetime_recorded" : ISODate("2013-05-01T12:35:13Z"),</code></li>
  <li><code>"datetime_recorded" : ISODate("2013-06-20T05:35:13Z"),</code></li>
  <li><code>"datetime_recorded" : ISODate("2013-01-17T07:35:13Z"),</code></li>
  <li><code>"datetime_recorded" : ISODate("2013-04-03T15:35:13Z"),</code></li>
  </ul>
  
  <p>a query </p>
  
  <pre><code>db.points.find({'datetime_recorded': {
      $gte: Date(1200 hours),
      $lt: Date(1600 hours)}
  });
  </code></pre>
  
  <p>would yield only the first and last point.</p>
  
  <p>Is this possible? Or would I have to do it for every day?</p>