List of usage examples for com.mongodb DBCursor DBCursor
DBCursor(final DBCollection collection, @Nullable final DBObject filter, final DBCollectionFindOptions findOptions, final boolean retryReads)
From source file:ezbake.data.mongo.HandlerForDriverFindCalls.java
License:Apache License
private QueryResultIterator handleMinCall(String collection, Object min, DBObject jsonQuery, ReadPreference readPref, EzSecurityToken token, AggregationOptions opts, List<DBObject> pipeline) throws Exception { Cursor cursor;// w w w . j av a 2 s .com QueryResultIterator qri;// make a agg call to get a QueryResultIterator instance // add $out to pipeline String outCollection = "min_out_" + randomAlphanumeric(8); pipeline.add(new BasicDBObject("$out", outCollection)); cursor = parent_handler.handler.db.getCollection(collection).aggregateWithOut(pipeline, opts, readPref); if (cursor instanceof QueryResultIterator) { qri = (QueryResultIterator) cursor; } else { appLog.info("UNKNOWN CURSOR RETURNED FOR handleMinCall agg call: {}", cursor.toString()); throw new Exception("$min converted to Aggregate pipeline did not return a QueryResultIterator: " + cursor.toString()); } for (String key : ((DBObject) min).keySet()) { // create the index for the keys parent_handler.handler.db.getCollection(outCollection).createIndex(new BasicDBObject(key, 1)); } cursor = new DBCursor(parent_handler.handler.db.getCollection(outCollection), new BasicDBObject(), new BasicDBObject(), ReadPreference.primary()).addSpecial("$min", min); List<DBObject> l = new ArrayList<>(); while (cursor.hasNext()) { DBObject o = cursor.next(); appLog.info("o {}", o); l.add(o); } qri.setIterator(l.iterator()); qri.setCurSize(l.size()); // drop the tmp output collection parent_handler.handler.db.getCollection(outCollection).drop(); return qri; }
From source file:ezbake.data.mongo.HandlerForDriverFindCalls.java
License:Apache License
private QueryResultIterator handleMaxCall(String collection, Object max, DBObject jsonQuery, ReadPreference readPref, EzSecurityToken token, AggregationOptions opts, List<DBObject> pipeline) throws Exception { Cursor cursor;/* ww w .j a v a 2 s.co m*/ QueryResultIterator qri;// make a agg call to get a QueryResultIterator instance // add $out to pipeline String outCollection = "max_out_" + randomAlphanumeric(8); pipeline.add(new BasicDBObject("$out", outCollection)); cursor = parent_handler.handler.db.getCollection(collection).aggregateWithOut(pipeline, opts, readPref); if (cursor instanceof QueryResultIterator) { qri = (QueryResultIterator) cursor; } else { appLog.info("UNKNOWN CURSOR RETURNED FOR handleMaxCall agg call: {}", cursor.toString()); throw new Exception("$max converted to Aggregate pipeline did not return a QueryResultIterator: " + cursor.toString()); } for (String key : ((DBObject) max).keySet()) { // create the index for the keys parent_handler.handler.db.getCollection(outCollection).createIndex(new BasicDBObject(key, 1)); } cursor = new DBCursor(parent_handler.handler.db.getCollection(outCollection), new BasicDBObject(), new BasicDBObject(), ReadPreference.primary()).addSpecial("$max", max); List<DBObject> l = new ArrayList<>(); while (cursor.hasNext()) { DBObject o = cursor.next(); l.add(o); } qri.setIterator(l.iterator()); qri.setCurSize(l.size()); // drop the tmp output collection parent_handler.handler.db.getCollection(outCollection).drop(); return qri; }
From source file:org.jongo.Find.java
License:Apache License
public <T> MongoCursor<T> map(ResultHandler<T> resultHandler) { DBCursor cursor = new DBCursor(collection, query.toDBObject(), getFieldsAsDBObject(), readPreference); for (QueryModifier modifier : modifiers) { modifier.modify(cursor);/* w ww . j a va2s. c o m*/ } return new MongoCursor<T>(cursor, resultHandler); }