$out in aggregation MongoDB

1.3k views Asked by At

Can anyone explain me why in Java when i do an aggregation pipeline with "$out" don't write the result in the new collection when i write only this:

     Document match = new Document("$match", new Document("top_speed",new  Document("$gte",350)));
     Document out=new Document("$out", "new_collection");
            coll.aggregate(Arrays.asList(
            match,out
            )
            );

When I save the aggregation result and I iterate on it, the new collection is created and the result of the match is inside (Java has an error obviously in this case):

    AggregateIterable<Document> resultAgg=
            coll.aggregate(Arrays.asList(
            match,out
            )
            );

    for (Document doc : resultAgg){

            System.out.println("The result of aggregation match:-"+    doc.toJson());
    }

I can't understand why.

1

There are 1 answers

0
D R On

You can call toCollection() method instead of iterating.

Document match = new Document("$match", new Document("top_speed", new Document("$gte", 350)));
Document out = new Document("$out", "new_collection");
coll.aggregate(Arrays.asList(match, out)).toCollection();