spring-data - 如何使用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);
}
}
}
解决方案
您可以尝试重命名findByStatusAndDateLessThan
为findFirst100000ByStatusAndDateLessThan
推荐阅读
- regex - VS 代码中的 Vim 正则表达式等价物
- json - 解析对象为变量的 JSON
- python - 在子图中循环生成两次图表
- android - React Native 0.55.2 Android 寻找 index.android.js
- php - 简单的 php 代码不打印到屏幕上
- algorithm - 查找整数列表的幂集的运行时复杂度
- dynamic - Google Apps 脚本 - google 表单“根据答案转到部分”
- angularjs - 使用 ui-router 在嵌套视图中传递参数
- java - Java+Discord - 为聊天叠加创建透明框架
- java - ReduceOps.makeRef 源代码中return 语句之后的这段代码有什么作用?