首页 > 解决方案 > 如何使用spring数据从mongodb加载记录

问题描述

我只想加载 100000 条在 mongodb 中处于 NOT_STARTED 状态的记录,并希望处理这些记录并将状态更新为 STARTED。我想重复这个过程,直到处理完所有处于 NOT_STARTED 状态的记录。

目前我正在使用 Pagerequest,如下面的代码所示,它似乎有效。但是有没有一种方法可以在没有 pagerequest 的情况下让我的存储库扩展 spring MongoRepository。因为 Pagerequest 似乎用于分页。但我没有做任何分页,每次只加载 100000 条记录并处理它们

Sort sort = new Sort(Sort.Direction.ASC, "_id");
int count = (int) PaymentReportRepository.count();
for(int i = 0; i < count; i += reportProperties.getPageSize()) {
List<PaymentReport> paymentReportList =
        MongoTraceability.capture(() ->
        PaymentReportRepository.findByStatusAndDateLessThan("NOT_STARTED",
                LocalDateTime.now().minusSeconds(reportProperties.getTimeInterval()),
                ,PageRequest.of(0, reportProperties.getPageSize(), sort)));

        if (paymentReportList != null && !paymentReportList.isEmpty()) {
            for (PaymentReport paymentReport : paymentReportList) {
                   messageService.processMessage(paymentReport);
            }
        }
}

标签: spring-dataspring-data-mongodb

解决方案


您可以尝试重命名findByStatusAndDateLessThanfindFirst100000ByStatusAndDateLessThan


推荐阅读