Example usage for com.mongodb.client MongoCollection find

List of usage examples for com.mongodb.client MongoCollection find

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection find.

Prototype

FindIterable<TDocument> find(ClientSession clientSession);

Source Link

Document

Finds all documents in the collection.

Usage

From source file:org.sead.monitoring.engine.SeadMon.java

License:Apache License

private static List<LogEvent> queryLog(MongoCollection collection, BasicDBObject query, String countStr,
        int start) {

    int count = 0;
    if (countStr != null && !countStr.equals(Constants.INFINITE))
        count = Integer.parseInt(countStr);
    start = start < 0 ? 0 : start;/*from  w w  w  . java 2s .co  m*/

    FindIterable<Document> iter;
    if (countStr == null || (countStr != null && countStr.equals(Constants.INFINITE))) {
        iter = collection.find(query).skip(start).sort(new BasicDBObject("date", 1));
    } else {
        iter = collection.find(query).limit(count).skip(start).sort(new BasicDBObject("date", 1));
    }
    List<LogEvent> logEvents = new ArrayList<LogEvent>();
    MongoCursor<Document> cursor = iter.iterator();
    try {
        while (cursor.hasNext()) {
            Document dbobj = cursor.next();
            //Converting BasicDBObject to a custom Class(LogEvent)
            LogEvent logEvent = (new Gson()).fromJson(dbobj.toJson(), LogEvent.class);
            logEvents.add(logEvent);
        }
    } finally {
        cursor.close();
    }
    return logEvents;
}