Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$out in aggregation MongoDB

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.

like image 673
Milaci Avatar asked Nov 10 '22 11:11

Milaci


1 Answers

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();
like image 68
Dzmitry Rudkouski Avatar answered Nov 15 '22 07:11

Dzmitry Rudkouski