Mongo DB: Iterate through a MongoCollection

This is specifically for Mongo 3.x, where MongoCollection is preferred over DBCollection. Adding for personal archiving.


MongoCollection<Document> mongoCollection =
            getMongoCollection(databaseName, collectionName);

        FindIterable<Document> documents =
            mongoCollection.find(eq(COLUMN, "lastName"))
                .sort(ascending("lastname"));

        for (Document document : documents) {
            ...
        }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.