简体   繁体   English

MongoDB Java驱动程序3.1转换为数组查找

[英]MongoDB Java driver 3.1 conver find to array

In the older mongo drivers we could convert a DBCursor to array. 在较早的mongo驱动程序中,我们可以将DBCursor转换为数组。 How can I accomplish it using mongo Java driver 3.1? 如何使用mongo Java驱动程序3.1完成此操作?

I have looked into mongo java driver api and found only this way: 我研究了mongo java驱动程序api并发现只有这种方式:

MongoHandler.getCollection().find().sort(docSort).limit(ConstMongo.LIMIT_PLAYERS)
                .forEach(new Block<Document>() {
                    @Override
                    public void apply(final Document document) {
                        arrResult.add(document.toJson());
                    }
                });

Is there a better and more clean way to do so? 有没有更好,更干净的方法呢? And if not, why did they change the way we can access array of documents? 如果不是,为什么他们改变了我们访问文档数组的方式?

In the Mongo 3.2 Java driver there is an into method that you can use to iterate over your results and put them into a collection 在蒙戈3.2 Java驱动程序有一个into的方法,你可以用它来遍历你的结果,并把它们放到一个集合

Example

List arrResult...
collection.find().into(arrResults);

See Java Doc http://api.mongodb.org/java/3.2/com/mongodb/client/MongoIterable.html#into-A- 参见Java Doc http://api.mongodb.org/java/3.2/com/mongodb/client/MongoIterable.html#into-A-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM