title: Get last created document in Mongodb using Spring Data repository
body: <p>I'm trying to get the created datetime of the last created item in a mongodb repository.</p>
  
  <p>I could obviously use a findAll(Sort sort) function, and get the first element, but this would not be very practical on a large database.</p>
  
  <p>Mongo queries do not support an "orderBy" query method so this is also not a solution.</p>
  
  <p>The order of creation is in chronological order of "created" so if I can get the last created document in the collection that would be good too.</p>
  
  <p>So my question is:
  What is the best way to retrieve the last created document in a mongodb repo using Spring data?</p>
  
  <p>My current code:</p>
  
  <pre><code>@Data
  @Document
  public class Batch
  {
      @Id
      String id;
      LocalDateTime created;
      //other stuff
  }
  
  
  public interface BatchRepository extends MongoRepository&lt;Batch,String&gt;
  {
      //this does not work
      //Batch findOneOrderByCreatedDesc();
  }
  </code></pre>