当前位置: 首页 > 面试题库 >

使用spring-data-mongodb传输聚合操作的结果

严欣怡
2023-03-14
问题内容

我正在使用spring-data-mongodb,并且想使用游标进行聚合操作。

MongoTemplate.stream() 得到一个查询,所以我试图创建聚合实例,将其转换为 DBOBJECT 使用
Aggregation.toDbObject() ,创建了一个 BasicQuery 使用 DBOBJECT ,然后调用 流() 方法。
这将返回一个空游标。

调试spring-data-mongodb代码显示 MongoTemplate.stream() 使用 FindOperation
,这使我认为thinkspring-data-mongodb不支持流式聚合操作。
有谁能够使用spring-data-mongodb来流式传输聚合查询的结果?

作为记录,我可以使用Java mongodb驱动程序来实现,但是我更喜欢使用spring-data。

编辑 11月10日-添加示例代码:

    MatchOperation match = Aggregation.match(Criteria.where("type").ne("AType"));
    GroupOperation group = Aggregation.group("name", "type");
    group = group.push("color").as("colors");
    group = group.push("size").as("sizes");
    TypedAggregation<MyClass> agg = Aggregation.newAggregation(MyClass.class, Arrays.asList(match, group));

    MongoConverter converter = mongoTemplate.getConverter();
    MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext = converter.getMappingContext();
    QueryMapper queryMapper = new QueryMapper(converter);
    AggregationOperationContext context = new TypeBasedAggregationOperationContext(MyClass.class, mappingContext, queryMapper);
    // create a BasicQuery to be used in the stream() method by converting the Aggregation to a DbObject
    BasicQuery query = new BasicQuery(agg.toDbObject("myClass", context));

    // spring-mongo attributes the stream() method to find() operationsm not to aggregate() operations so the stream returns an empty cursor
    CloseableIterator<MyClass> iter = mongoTemplate.stream(query, MyClass.class);

    // this is an empty cursor
    while(iter.hasNext()) {
        System.out.println(iter.next().getName());
    }

以下代码未使用stream()方法,返回了预期的聚合非空结果:

    AggregationResults<HashMap> result = mongoTemplate.aggregate(agg, "myClass", HashMap.class);

问题答案:

对于那些仍在寻找答案的人:

从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);


 类似资料:
  • 我使用的是spring data mongodb,我想使用光标进行聚合操作。 MongoTemplate.stream()得到一个查询,所以我尝试创建聚合实例,使用ggregation.toDbObject()将其转换为DbObject,使用DbObject创建BasicQuery,然后调用stream()方法。 这返回一个空光标。 调试spring数据mongodb代码显示MongoTempla

  • 我需要使用spring数据过滤mongodb中的文档,该数据包含nexted数组。我在mongo shell上使用以下聚合查询,它运行良好。但当我通过springdata聚合操作触发该操作时,我得到的是空响应。mongo查询的工作原理是: 我正在使用但不工作的Spring数据代码: 我尝试打破这个聚合函数,它能够项目rssSearchResponse.journeys但$unWind后,它返回空结

  • 我有一套文件。每个文档有两个字段—“代码”和“状态”。我的mongodb集合包含以下文档: 我想按每个代码的状态查找计数。我想要的输出如下所示: 如何使用spring data mongodb实现这一点?我对mongodb很陌生。 更新我已成功编写mongodb查询。这是: 有人能帮助您在spring data mongodb中编写此查询吗?

  • 问题内容: 我有一个聚合管道,其中包括一个像这样的项目: 在mongo shell中可以正常工作。如何在Spring-Mongodb中使用Aggregation框架表达这一点?我见过ProjectionOperationBuilder,ExpressionProjectionOperationBuilder类型,但没有一个示例如何使用它们……有什么建议吗? 问题答案: 如果使用通过管道支持操作员的

  • 我不熟悉Spring和Mongo。我正在使用Spring Batch获取一些报告。我的查询需要一些MongoItemReader不支持的聚合,所以我根据下面的stackoverflow链接扩展了该类。 如何在Spring批处理中使用MongoItemReader聚合查询 但我的聚合有点问题。我制作的聚合在mongoDB中运行良好,但无法将其转换为Spring mongo聚合。 MongoDb聚合按

  • 我需要筛选此查询,以便不显示值为的文档。当前,我的代码显示以下结果: 我需要一些过滤器,使消失,以便它显示我作为第一个值:。 我以前执行过以下操作: