【问题标题】:Get max date from collection in mongoDB by Spring Boot repository通过 Spring Boot 存储库从 mongoDB 中的集合中获取最大日期
【发布时间】:2020-12-29 06:52:21
【问题描述】:

我需要从 Spring Boot 存储库的 MongoDB(指南针)集合中获取最大日期,并且字段“sendingSystemCode”需要等于“999”。

现在我是这样做的:

@Repository
public interface ControlFutureTransactionRepository extends 
MongoRepository<ControlFutureTransactionEntity, Integer>
{
    @Query("{'sendingSystemCode': {$eq: 999}}")
    List<ControlFutureTransactionEntity> getControlFutureTransactionBySendingSystemCode();
}

我通过代码中的一个简单循环获得的最大日期,我想通过一个查询使代码更快,将最大日期添加到存储库中的方法。 由于某种原因,这个选项在 google 中很难找到。

非常感谢!

【问题讨论】:

    标签: java mongodb spring-boot repository


    【解决方案1】:

    你有两个选择:

    选项 1(聚合框架):

    db.collection.aggregate([ {$match:{sendingSystemCode:999}} , {$group:{_id:"the_max_date" ,maxDate:{$max:"$createdDate"}}}        ])
    

    选项 2(查找/排序/限制):

    db.collection.find({sendingSystemCode:999}).sort({createdDate:-1}).limit(1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 2015-11-11
      • 1970-01-01
      • 2012-05-06
      • 2012-06-18
      • 2021-12-11
      • 2015-12-12
      相关资源
      最近更新 更多