List of usage examples for com.mongodb.client MongoCollection find
FindIterable<TDocument> find(ClientSession clientSession);
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; }