@Override public K fetchFirst() { try { DBCursor c = createCursor().limit(1); if (c.hasNext()) { return transformer.apply(c.next()); } else { return null; } } catch (NoResults ex) { return null; } }
private Timestamp<?> getCurrentOplogTimestamp() { try (DBCursor cursor = oplogCollection.find().sort(new BasicDBObject(MongoDBRiver.INSERTION_ORDER_KEY, -1)).limit(1)) { return Timestamp.on(cursor.next()); } }
protected <T extends Persisted> List<DBObject> query(Class<T> modelClass, DBObject query, DBObject sort, int limit, int offset) { return cursorToList( collection(modelClass) .find(query) .sort(sort) .limit(limit) .skip(offset) ); }
private Timestamp<?> getCurrentOplogTimestamp(MongoClient shardClient) { DBCollection oplogCollection = shardClient .getDB(MongoDBRiver.MONGODB_LOCAL_DATABASE) .getCollection(MongoDBRiver.OPLOG_COLLECTION); try (DBCursor cursor = oplogCollection.find().sort(new BasicDBObject("$natural", -1)).limit(1)) { return Timestamp.on(cursor.next()); } }
@Override public K fetchOne() throws NonUniqueResultException { try { Long limit = queryMixin.getMetadata().getModifiers().getLimit(); if (limit == null) { limit = 2L; } DBCursor c = createCursor().limit(limit.intValue()); if (c.hasNext()) { K rv = transformer.apply(c.next()); if (c.hasNext()) { throw new NonUniqueResultException(); } return rv; } else { return null; } } catch (NoResults ex) { return null; } }
/** * Returns the first document that matches the query. * * @return the first matching document * @since 2.12 */ @Nullable public DBObject one() { DBCursor findOneCursor = copy().limit(-1); try { return findOneCursor.hasNext() ? findOneCursor.next() : null; } finally { findOneCursor.close(); } }
protected DBCursor createCursor(DBCollection collection, @Nullable Predicate where, Expression<?> projection, QueryModifiers modifiers, List<OrderSpecifier<?>> orderBy) { DBCursor cursor = collection.find(createQuery(where), createProjection(projection)); Integer limit = modifiers.getLimitAsInteger(); Integer offset = modifiers.getOffsetAsInteger(); if (limit != null) { cursor.limit(limit); } if (offset != null) { cursor.skip(offset); } if (orderBy.size() > 0) { cursor.sort(serializer.toSort(orderBy)); } if (readPreference != null) { cursor.setReadPreference(readPreference); } return cursor; }
c.skip(firstResult).limit(maxResult); while (c.hasNext())
/** * Limits the number of elements returned. * Note: parameter <tt>n</tt> should be positive, although a negative value is supported for legacy reason. * Passing a negative value will call {@link DBCursor <T>#batchSize(int)} which is the preferred method. * * @param n the number of elements to return * @return a cursor to iterate the results */ public DBCursor<T> limit(int n) { cursor.limit(n); return this; }
c = c.limit(q.getMaxFeatures());
: dbCollection.find(mongoQuery, keys).limit(maxResult).skip(firstResult);
@Override public DBCursor limit(int n) { log.debug("--"+cursor.getCollection().getName()+"-> limit="+n); return cursor.limit(n); }
private boolean resultNotEmpty(final DBObject query, final String indexName) { try { DBCursor cursor = getCollection().find(query).limit(1); if(ensureIndex && !isEmpty(indexName)) cursor.hint(indexName); return cursor.size() > 0; } catch (MongoException mongoException) { LOG.error("Got error {} with query: {} using hint: {}", mongoException.getMessage(), query, indexName); throw mongoException; } }
@Override public Object doRead(String deviceId, String metric) { BasicDBObject metricQuery = new BasicDBObject(of(FIELD_DEVICE_ID, deviceId, FIELD_METRIC, metric)); DBCursor cursor = mongo.getDB(db).getCollection(collection).find(metricQuery).sort(new BasicDBObject("_id",-1)).limit(1); return cursor.hasNext() ? cursor.next().get("value") : null; }
/** * @param coll * @param object * @return */ @Override public DBCollection apply(DBCollection coll, DBObject object) { List<DBObject> objects = coll.find().limit(((Number) object.get(getKeyword())).intValue()).toArray(); return dropAndInsert(coll, objects); }
protected <T extends Persisted> List<DBObject> query(Class<T> modelClass, DBObject query, DBObject sort, int limit, int offset) { return cursorToList( collection(modelClass) .find(query) .sort(sort) .limit(limit) .skip(offset) ); }
private List<SentMessageTrace> getLastUndeliveredMessages(String topicName, String subscriptionName, SentMessageTraceStatus status) { try ( DBCursor cursor = database.getCollection(COLLECTION_SENT_NAME) .find(new BasicDBObject(TOPIC_NAME, topicName) .append(LogSchemaAware.SUBSCRIPTION, subscriptionName).append(STATUS, status.toString())) .sort(new BasicDBObject(TIMESTAMP, -1)).limit(1) ) { return StreamSupport.stream(cursor.spliterator(), false) .map(this::convert) .collect(Collectors.toList()); } }
private void prepareCursor(){ this.cursor = this.collection .find(getQuery(),getProjection()) .sort(getSort()) .limit(getLimit()); }
@Override public Page<Record> fetchRecords(StoreQuery query) { DBObject mongoQuery = convert(query); final BasicDBObject projection = query.isMetadataOnly() ? new BasicDBObject(BatchConstants.metadataKey, 1) : null; DBCursor cursor = accessor.getCollection().find(mongoQuery, projection); cursor.sort(createOrderBy(query)); cursor = cursor.skip(query.getPageSize() * query.getPageNo()); cursor = cursor.limit(query.getPageSize()); return convert(cursor, query); }