使用spring-data-mongodb传输聚合操作的结果 [英] Streaming the result of an aggregate operation using spring-data-mongodb

查看:451
本文介绍了使用spring-data-mongodb传输聚合操作的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using spring-data-mongodb and I want to use a cursor for an aggregate operation.

MongoTemplate.stream()获取查询,因此我尝试创建Aggregation实例,并使用 Aggregation.toDbObject()将其转换为 DbObject ,使用 DbObject 创建了 BasicQuery ,然后调用 stream()方法.
这将返回一个空光标.

MongoTemplate.stream() gets a Query, so I tried creating the Aggregation instance, convert it to a DbObject using Aggregation.toDbObject(), created a BasicQuery using the DbObject and then invoke the stream() method.
This returns an empty cursor.

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

Debugging the spring-data-mongodb code shows that MongoTemplate.stream() uses the FindOperation, which makes me thinkspring-data-mongodb does not support streaming an aggregation operation.
Has anyone been able to stream the results of an aggregate query using spring-data-mongodb?

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

For the record, I can do it using the Java mongodb driver, but I prefer using spring-data.

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

EDIT Nov 10th - adding sample code:

    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()方法)返回聚合的预期非空结果:

The following code, not using the stream() method, returns the expected non-empty result of the aggregation:

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

推荐答案

对于仍在尝试找到答案的人:

For those who are still trying to find the answer to this:

从spring-data-mongo版本2.0.0.M4开始( AFAIK ),MongoTemplate获得了aggregateStream方法.

From spring-data-mongo version 2.0.0.M4 onwards (AFAIK) MongoTemplate got an aggregateStream method.

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

So you can do the following:

 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传输聚合操作的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆