title: Mongodb Query: Latest record by date for each item
body: <p>There are six devices in a collection, each has many records, some have records of new dates and some have week or/and month older. I need a query which returns latest last record of each device. In the case of .aggregate() I need that complete "data" filed.</p>
  
  <p>Here is the sample json.</p>
  
  <pre><code>{
  "date_time" : some-date
  "device_id" : 27,
  "gateway_id" : 1,
  "data": [{"r" : 203,"v" : 3642},{"r" : 221,"v" : 3666}]
  }
  {
  "date_time" : some-date
  "device_id" : 28,
  "gateway_id" : 1,
  "data": [{"r" : 203,"v" : 3002},{"r" : 221,"v" : 3006}]
  }
  {
  "date_time" : some-date
  "device_id" : 29,
  "gateway_id" : 1,
  "data": [{"r" : 203,"v" : 3002}, {"r" : 221,"v" : 3006}]
  }
  </code></pre>
  
  <p>I tried a lot of queries but no success.</p>
  
  <p>Please not the following are not for this case.</p>
  
  <pre><code>db.col.find({"device_id": {"$in": devices}, "date_time": { "$gte": last_date}}) .sort({$natural: -1})
  
  db.col.find({"device_id": {"$in": devices}, "date_time": { "$gte": last_date}}) .sort({$natural: -1}).limit(1)
  
  db.col.find({"device_id": {"$in": devices}}) .sort({"date_time": -1}).limit(1)
  
  db.col.find({"device_id": {"$in": devices}}) .sort({"_id": -1}).limit(1)
  </code></pre>
  
  <p>I am looking for the query which provides only latest record of every device in collection. Please note that in the case of .aggregate() I need  that complete "data" filed.</p>