title: How to use MongoDB to aggregate Reports for time periods within multiple days?
body: <p>I have pre-aggregated reports following the mongodb docs, see: 
  <a href="https://docs.mongodb.org/ecosystem/use-cases/pre-aggregated-reports/#one-document-per-page-per-day" rel="nofollow">https://docs.mongodb.org/ecosystem/use-cases/pre-aggregated-reports/#one-document-per-page-per-day</a></p>
  
  <pre><code>{
      _id: "20101010/site-1/apache_pb.gif",
      metadata: {
          date: ISODate("2000-10-10T00:00:00Z"),
          site: "site-1",
          page: "/apache_pb.gif" },
      daily: 5468426,
      hourly: {
          "0": 227850,
          "1": 210231,
          ...
          "23": 20457 },
      minute: {
          "0": 3612,
          "1": 3241,
          ...
          "1439": 2819 }
  }
  </code></pre>
  
  <p>I aggregate the views for x days back like this:</p>
  
  <pre><code>db.collection.aggregate(
  { $match : { date: { $gte : ISODate("2016-04-07T00:00:00.000Z")}}},
  { $group: {_id: "_id", views: {$sum: "$daily"}}}, 
  { $sort: {views: -1}}    
  </code></pre>
  
  <p>)</p>
  
  <p>Now I want the views of the last x hours. The problem here is the date line. As long as I' m within the same day my solution works fine, but how to handle crossing the date line the best way?</p>
  
  <p>Here is what I have:</p>
  
  <pre><code>db.collection.aggregate(
  { $match : { date: { $eq : ISODate("2016-04-07T00:00:00.000Z")}}},
  { $group: {_id: "$_id", views: { $sum: { $add: [ '$hourly.1', '$hourly.2', ... ] }} }}, 
  { $sort: {views: -1}}    
  </code></pre>