title: Mongo aggregation: partitioning values into groups
body: <p>This is probably a longshot, but:</p>
  
  <p>I'd like to group a set of time-series documents by gaps between dates: sort the documents ascending by date, then partition when the interval between the current and previous is above some threshold.</p>
  
  <p>I can do this easily after getting the documents, of course; in this example, the original documents get a new partition number field:</p>
  
  <pre><code>// assuming sorted docs
  var partition = 0;
  var partitioned = docs.map((e,i) =&gt; {
          if(i &gt; 0)
              if(e.date - docs[i-1].date &gt; minInterval) partition++;
  
          return {
              date: e.date,
              partition: partition
          }
      });
  </code></pre>
  
  <p>But I don't actually need the documents themselves, I just need the first and last dates and number of docs for each partition. It's just unclear how I would do the partitioning function.</p>
  
  <p>Is this possible with an aggregation? I see a <a href="https://jira.mongodb.org/browse/SERVER-29161" rel="nofollow noreferrer">possibly relevant Mondo ticket</a> that is open, so I'm guessing not.</p>