MongoDB Java 驱动程序聚合字符串

MongoDB Java driver aggregate String

我得到一个结构为 JSONArray [ { "abc" : "123" }, { "def" : "456" } ] 的字符串,我需要用它来调用 mongoCollection.aggregate(theString);

聚合函数采用 List<? extends Bson>,我不确定将字符串转换为 List<? extends Bson> 的最佳方法是什么。

对于采用 Bson var1 的 find() 方法,我只是使用 Document.parse(theString);

将字符串转换为文档

我正在使用 mongodb 3.4.

我想出了这个,但它看起来有点丑。

        JSONArray array = new JSONArray(theString);
        List<Document> list = new ArrayList<>();
        for(Object jsonObject : jsonArray){
            Document document = Document.parse(jsonObject.toString());
            list.add(document);
        }
        collection.aggregate(list);