相关疑难解决方法(0)

执行 MongoTemplate.aggregate 而不进行行检索

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

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

有没有办法使用 MongoTemplate.aggregate 跳过行检索?

前任:

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
);
Run Code Online (Sandbox Code Playgroud)

java spring mongodb spring-mongo spring-mongodb

4
推荐指数
1
解决办法
1647
查看次数

标签 统计

java ×1

mongodb ×1

spring ×1

spring-mongo ×1

spring-mongodb ×1