5

谁能解释我为什么在Java中当我用“$out”做一个聚合管道时,当我只写这个时,不要把结果写到新集合中:

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

当我保存聚合结果并对其进行迭代时,会创建新集合并且匹配的结果在里面(Java 在这种情况下显然有错误):

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

    for (Document doc : resultAgg){

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

我不明白为什么。

4

1 回答 1

1

您可以调用toCollection()方法而不是迭代。

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();
于 2019-08-22T14:30:22.643 回答