Return value of mongodb aggregation with $out as the last stage in aggregation pipeline

I want to execute a mongodb aggregation pipeline with Spring Data Mongodb. The last step in that pipeline is writing out all documents to the collection targetCollection in the same database.

I implemented the pipeline with an @Aggregation annotation in my MongoRepository:

public interface PersonRepository extends MongoRepository<Person, String> {

@Meta(allowDiskUse = true)
@Aggregation(pipeline = {"{$group: { .... } }", "{$out: 'targetCollection'}" })
Stream<Person> deduplicateToTargetCollection();


As the Collection has a significant number of Documents I don’t want the Documents of the new Collection I am writing to with this aggregation pipeline to be transferred from mongodb to the Java application.

However, putting a void return type on the method signature of deduplicateToTargetCollection() does not work. So, the best I could come up with was setting the return type to Stream .

Is the assumption correct that if I just close that Stream and don’t query it for its elements there isn’t any transfer of the Documents in the newly created Collection to the Spring Data Mongodb Java application that executes the aggregation? Does anyone know whether this is documented anywhere to confirm?

Much appreciate any help here.

There is no mechanism for this yet. The issue is being tracked in the Spring Data MongoDB Github project here:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.