Spring Data MongoDb如何实现按批次查找

时间:2019-03-06 02:37:36

标签: mongodb spring-data-mongodb

我正在使用spring数据mongodb,并且我有一个非常大的集合,其中包含约10万多个文档。

我需要分批查询,以免出现内存问题。我打算到100点。

等效于MongoDb查询:

db.getCollection('my_access_log')
    .find({'creationDate':{$gt:'2019-02-13T03:32:00', $lt:'2019-03-05T03:32:00'},  taskId:null})
        .batchSize(100)

在这里,我想获取介于taskId为null的指定日期时间之间的日志/文档。

样本数据:

{
    "_id" : "7e8e5e76-f5bc-4ff4-a329-30d132e135d3",
    "creationDate" : "2019-02-13T08:31:42",
    "cardId" : "980cac01f7ff1123",
    "sn": "123-abc-456-def",
    "taskId": null
}

型号:

public class MyAccessLog {
    @Id
    private String id;

    private String sn;

    private String cardId;

    private String taskId;

    private DateTime creationDate;

    // getters and setters
}

道:

public interface MyAccessLogDao extends MongoRepository<MyAccessLog, String> {

    @Query("{ 'sn': ?0, 'creationDate': { $gte: ?1}, 'taskId': { $exists: false} }")
    Collection<MyAccessLog> queryUnprocessedAccessLogs(String sn, DateTime creationDate);

}

问题: 该查询有效,但结果约为22k + ...我想按批处理查询,例如在指示的mongo查询中用100说。

如何调整或更改spring data mongo中的查询以分批查询?

谢谢!

0 个答案:

没有答案