Spring MongoTemplateexecuteCommand 结果超过第一批

问题描述 投票:0回答:1

我正在使用 spring-data-mongodb 3.3.1 运行本机 MongoDB 查询,如下所示:

String jsonCommand = "{ aggregate: 'CollectionName', pipeline: [ { $match: ...... ] , cursor: { } };
Document commandResult = mongoTemplate.executeCommand(jsonCommand);
List<Document> documents = (List<Document>) ((Document) commandResult.get("cursor")).get("firstBatch");

查询返回101个文档,这是默认的batchSize。我的问题是,查询结果不包含所有匹配的文档,我想重复此查询,直到获得所有匹配的文档。我的问题是:

如何检查“firstBatch”之外是否还有更多文档,如果有,则重复相同的查询,直到获得所有结果?

我知道我可以硬编码更大的batchSize,如下所示:

cursor: { batchSize: 10000 }

但这不是我想要的。我想知道如何批量检索文档。

如有任何帮助,我们将不胜感激。

java mongodb spring-data-mongodb
1个回答
0
投票

你尝试过 cmd.put("skip",100) 吗?

© www.soinside.com 2019 - 2024. All rights reserved.