title: MongoDB Aggregation Facet to Bucket by Days in a Date Range
body: <p>How can I bucket documents in MongoDB 3.4.1 with two fields representing a date range into the days that are covered by that range?</p>
  
  <p>For example, I have the following documents:</p>
  
  <pre><code>{ start: ISODate('2017-04-01T12:30:00Z'), end: ISODate('2017-04-02T12:30:00Z') },
  { start: ISODate('2017-04-04T12:00:00Z'), end: ISODate('2017-04-04T12:30:00Z') },
  { start: ISODate('2017-04-04T12:30:00Z'), end: ISODate('2017-04-08T12:30:00Z') },
  { start: ISODate('2017-04-05T12:30:00Z'), end: ISODate('2017-04-06T12:30:00Z') },
  { start: ISODate('2017-04-08T12:30:00Z'), end: ISODate('2017-04-09T12:30:00Z') }
  </code></pre>
  
  <p>and I'm looking to bucket those into the following histogram:</p>
  
  <pre><code>[
    { '2017-04-01': 1 },
    { '2017-04-02': 1 },
    { '2017-04-04': 2 },
    { '2017-04-05': 2 },
    { '2017-04-06': 2 },
    { '2017-04-07': 1 },
    { '2017-04-08': 2 },
    { '2017-04-09': 1 }
  ]
  </code></pre>
  
  <p>If a document's date range covers any portion of a day, the document should count for 1 item on that day.</p>
  
  <p>I'd like to perform this bucketing with a single aggregation pipeline, if possible. This is fairly straightforward with individual timestamp fields by projecting them with a day formatter such as:</p>
  
  <pre><code>{ $project: { day: { $dateToString: { format: '%Y-%m-%d', date: '$start' } } } }
  </code></pre>
  
  <p>but it does not seem as those there are aggregation operators that support this for a date range.</p>