如何使用 Java 迭代文档对象并避免错误"can only iterate over an array or an instance of java.lang.Iterable"?


while ((line = br.readLine()) != null) {
         Document document = Document.parse(String.format("{"a": %s}",line)); for(Document doc : document.get("a")) {
               docs.add(new InsertOneModel<>(doc));
           }

我在for循环for (Document doc : document.get("a"))中遇到错误,

只能在数组或Java.lang.iterable

的实例上迭代

如何循环浏览文档并避免此错误?

如果您简单地将for循环更改为

for (Document doc : document.get("a").toArray()) {
}

相关内容

最新更新