title: Mogodb split values into 5 minute intervals and return most recent within interval group
body: <p>My Mongo database has documents as so:</p>
  
  <pre><code>{
     "timestamp": ISODate("2015-09-27T15:28:06.0Z"),
      "value": '123'
  },
  {
     "timestamp": ISODate("2015-09-27T15:31:06.0Z"),
      "value": '737'
  },
  {
     "timestamp": ISODate("2015-09-27T15:35:00.0Z"),
      "value": '456'
  },
  {
     "timestamp": ISODate("2015-09-27T15:40:20.0Z"),
      "value": '789'
  }
  </code></pre>
  
  <p>...etc...</p>
  
  <p>What I want to do is aggregate these in 5 minute intervals and than get the most recent (with the latest timestamp) value <strong>per 'group of 5 minutes'</strong>.</p>
  
  <p>So basically the steps are:</p>
  
  <p>1) split into groups of 5 minutes</p>
  
  <p>2) return the 5-minute timestamp and the value of the document that has the newest timestamp within this 5 minute group</p>
  
  <p>Based on that and my documents above the documents returned should be:</p>
  
  <pre><code>{
      "timestamp": ISODate("2015-09-27T15:25:00.0Z"),
      "value": '123'
  },
  {
      "timestamp": ISODate("2015-09-27T15:35:00.0Z"),
      "value": '456' // 456 has a newer timestamp than 737, which are in the same 5 minute range
  },
  {
      "timestamp": ISODate("2015-09-27T15:40:00.0Z"),
      "value": '789'
  }
  </code></pre>
  
  <p>I have tried grouping into 5 minute intervals as described here: <a href="https://stackoverflow.com/a/26814496/1007236">https://stackoverflow.com/a/26814496/1007236</a></p>
  
  <p>Starting from there I can't find out how to return the value of the most recent within each 5 minute group.</p>
  
  <p>How can I do that?</p>