title: MongoDB Aggregate - Query to get most recent item in group
body: <p>Given this source data:</p>
  
  <pre><code>{ "_id" : ObjectId("1"), "productID" : 1, "amount" : 1, "date" : ISODate("2017-02-01T00:00:00Z") }
  { "_id" : ObjectId("2"), "productID" : 2, "amount" : 2, "date" : ISODate("2017-02-01T00:00:00Z") }
  { "_id" : ObjectId("3"), "productID" : 3, "amount" : 3, "date" : ISODate("2017-02-01T00:00:00Z") }
  { "_id" : ObjectId("4"), "productID" : 4, "amount" : 4, "date" : ISODate("2017-02-01T00:00:00Z") }
  { "_id" : ObjectId("5"), "productID" : 1, "amount" : 11, "date" : ISODate("2017-02-02T00:00:00Z") }
  { "_id" : ObjectId("6"), "productID" : 2, "amount" : 22, "date" : ISODate("2017-02-02T00:00:00Z") }
  { "_id" : ObjectId("7"), "productID" : 2, "amount" : 222, "date" : ISODate("2017-02-03T00:00:00Z") }
  { "_id" : ObjectId("8"), "productID" : 3, "amount" : 33, "date" : ISODate("2017-02-03T00:00:00Z") }
  </code></pre>
  
  <p>I want to get the most recent record for each product (keyed on <code>productID</code>) and print out the full row for that record per product sorted on <code>productID</code>. So my desired output from above would be:</p>
  
  <pre><code>{ "_id" : ObjectId("5"), "productID" : 1, "amount" : 11, "date" : ISODate("2017-02-02T00:00:00Z") }
  { "_id" : ObjectId("7"), "productID" : 2, "amount" : 222, "date" : ISODate("2017-02-03T00:00:00Z") }
  { "_id" : ObjectId("8"), "productID" : 3, "amount" : 33, "date" : ISODate("2017-02-03T00:00:00Z") }
  { "_id" : ObjectId("4"), "productID" : 4, "amount" : 4, "date" : ISODate("2017-02-01T00:00:00Z") }
  </code></pre>
  
  <p>I'm new to Mongo and having problems, managed to get most of it but couldn't delete the other older records for each product.</p>