当前位置: 首页 > 知识库问答 >
问题:

使用sping-data-mongodb流式传输聚合操作的结果

阴焱
2023-03-14

我使用的是spring data mongodb,我想使用光标进行聚合操作。

MongoTemplate.stream()得到一个查询,所以我尝试创建聚合实例,使用ggregation.toDbObject()将其转换为DbObject,使用DbObject创建BasicQuery,然后调用stream()方法。
返回一个空光标。

调试spring数据mongodb代码显示MongoTemplate。stream()使用FindOperation,这让我觉得Spring data mongodb不支持流式聚合操作
有人能够使用spring data mongodb流式传输聚合查询的结果吗?

作为记录,我可以使用Java mongodb驱动程序,但我更喜欢使用spring数据。

编辑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);

共有1个答案

刘松
2023-03-14

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

来自spring data mongo 2.0.0版。M4 Forwards(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() 得到一个查询,所以我试图创建聚合实例,将其转换为 DBOBJECT 使用 Aggregation.toDbObject() ,创建了一个 BasicQuery 使用 DBOBJECT ,然后调用 流() 方法。 这将返回一个空游标。 调试spring-data-

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

  • 我使用$geonear作为聚合框架的第一步。我需要过滤掉基于“标签”字段的结果,它工作得很好,但我看到有两种方式都给出了不同的结果。 MongoDB文档示例 我已经向“Position”键添加了2dSphere索引 两个查询返回的totalDocs似乎不同。 有人能给我解释一下这两个查询之间的区别吗?

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

  • 问题内容: 我正在开发一个使用大型MySQL表的spring应用程序。加载大表时,我得到一个,因为驱动程序试图将整个表加载到应用程序内存中。 我尝试使用 但是然后我打开的每个ResultSet都挂了; 在网上查看时,我发现发生这种情况是因为它尝试在关闭ResultSet之前尝试加载所有未读的行,但事实并非如此,因为我这样做是: 小表(3行)也会发生挂起,如果我不关闭RecordSet(在一种方法中

  • 本文向大家介绍Sanic框架流式传输操作示例,包括了Sanic框架流式传输操作示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Sanic框架流式传输操作。分享给大家供大家参考,具体如下: 简介 Sanic是一个类似Flask的Python 3.5+ Web服务器,它的写入速度非常快。除了Flask之外,Sanic还支持异步请求处理程序。这意味着你可以使用Python 3.5中新的闪亮的