执行MongoTemplate.aggregate,而无需检索行

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

我正在使用Spring Mongo驱动程序来执行一条大型mongo聚合语句,该语句将运行一段时间。此聚合的输出阶段将聚合的输出写入新的集合。我绝对不需要在内存中检索此聚合的结果。

当我在Spring Boot中运行此程序时,JVM进行行检索时内存不足,尽管我没有使用或存储任何结果。

是否可以使用MongoTemplate.aggregate跳过行检索?

Ex:

mongoTemplate.aggregate(Aggregation.newAggregation(
   Aggregation.sort(new Sort(new Sort.Order(Sort.Direction.DESC, "createdOn"))),

   Aggregation.group("accountId")
      .first("bal").as("bal")
      .first("timestamp").as("effectiveTimestamp"),

   Aggregation.project("_id", "effectiveTimestamp")
                        .andExpression("trunc(bal * 10000 + 0.5) / 100").as("bal"),

   aggregationOperationContext -> new Document("$addFields", new Document("history",Arrays.asList(historyObj))),

   // Write results out to a new collection - Do not store in memory
   Aggregation.out("newBalance")
).withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).build()),
   "account", Object.class
);
java spring mongodb spring-mongodb spring-mongo
1个回答
0
投票

我能够使用MongoTempalte.aggregateStream(...).withOptions(Aggregation.newAggregationOptions().cursorBatchSize(0).build)解决此问题>

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