简体   繁体   中英

MongoDB Java driver 3.1 conver find to array

In the older mongo drivers we could convert a DBCursor to array. How can I accomplish it using mongo Java driver 3.1?

I have looked into mongo java driver api and found only this way:

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

Example

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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