【发布时间】:2016-08-02 09:42:00
【问题描述】:
我正在尝试获取 mongodb 存储库中最后创建项目的创建日期时间。
我显然可以使用 findAll(Sort sort) 函数并获取第一个元素,但这在大型数据库上不太实用。
Mongo 查询不支持“orderBy”查询方法,因此这也不是解决方案。
创建的顺序是按“创建”的时间顺序排列的,所以如果我能得到集合中最后创建的文档也很好。
所以我的问题是: 使用 Spring 数据在 mongodb 存储库中检索最后创建的文档的最佳方法是什么?
我当前的代码:
@Data
@Document
public class Batch
{
@Id
String id;
LocalDateTime created;
//other stuff
}
public interface BatchRepository extends MongoRepository<Batch,String>
{
//this does not work
//Batch findOneOrderByCreatedDesc();
}
【问题讨论】:
标签: spring mongodb spring-data