【问题标题】:Cursor with Spring Data Mongo Aggregation带有 Spring Data Mongo 聚合的光标
【发布时间】:2017-03-03 23:29:45
【问题描述】:

有没有办法用spring data的mongodb聚合返回游标?

Aggregation agg = newAggregation(
            match(Criteria.where("_id").is(objId)),
            unwind("taskResultContent"),
            project("taskResultContent.executionUUID","taskResultContent.returnContent","taskResultContent.sequency").and("resultID").previousOperation(),
            match(Criteria.where("executionUUID").is(executionUUID)),
            sort(DESC,"sequency")
        ).withOptions(Aggregation.newOptions().cursor(cursor).build());

【问题讨论】:

    标签: mongodb aggregation-framework


    【解决方案1】:

    Solution 在此引用:

    从 spring-data-mongo 版本 2.0.0.M4 开始 (AFAIK) MongoTemplate 得到了一个 aggregateStream 方法。

    因此您可以执行以下操作:

     AggregationOptions aggregationOptions = Aggregation.newAggregationOptions()
        // this is very important: if you do not set the batch size, 
        // you'll get all the objects at once and you might run out of memory
        // if the returning data set is too large
        .cursorBatchSize(mongoCursorBatchSize)
        .build();
    
    data = mongoTemplate.aggregateStream(Aggregation.newAggregation(
           Aggregation.group("person_id")
                      .count()
                      .as("count"))
                      .withOptions(aggregationOptions), collectionName, YourClazz.class);
    

    【讨论】:

      【解决方案2】:

      mongodb 的 Spring 数据在聚合时不支持使用游标。必须改用 MongoDB java 驱动程序。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-02
      • 2019-11-27
      • 2020-01-28
      • 1970-01-01
      • 2019-10-14
      • 2020-04-29
      • 1970-01-01
      • 2022-01-27
      相关资源
      最近更新 更多