Example usage for com.mongodb DBCursor skip

List of usage examples for com.mongodb DBCursor skip

Introduction

In this page you can find the example usage for com.mongodb DBCursor skip.

Prototype

public DBCursor skip(final int numberOfElements) 

Source Link

Document

Discards a given number of elements at the beginning of the cursor.

Usage

From source file:net.ymate.platform.persistence.mongodb.impl.MongoGridFSSession.java

License:Apache License

public List<GridFSDBFile> findAll(OrderBy orderBy, Page page) {
    DBCursor _cursor = __dbCollection.find();
    if (orderBy != null) {
        _cursor.sort(orderBy.toBson());//www.  ja  va  2 s.  c  o  m
    }
    if (page != null && page.page() > 0 && page.pageSize() > 0) {
        _cursor.skip((page.page() - 1) * page.pageSize()).limit(page.pageSize());
    }
    List<GridFSDBFile> _results = new ArrayList<GridFSDBFile>();
    while (_cursor.hasNext()) {
        _results.add((GridFSDBFile) _cursor.next());
    }
    _cursor.close();
    return _results;
}

From source file:net.ymate.platform.persistence.mongodb.impl.MongoGridFSSession.java

License:Apache License

public List<GridFSDBFile> find(Query query, OrderBy orderBy, Page page) {
    DBCursor _cursor = __dbCollection.find(query.toBson());
    if (orderBy != null) {
        _cursor.sort(orderBy.toBson());/*w  w  w .  j av  a 2s  .c o  m*/
    }
    if (page != null && page.page() > 0 && page.pageSize() > 0) {
        _cursor.skip((page.page() - 1) * page.pageSize()).limit(page.pageSize());
    }
    List<GridFSDBFile> _results = new ArrayList<GridFSDBFile>();
    while (_cursor.hasNext()) {
        _results.add((GridFSDBFile) _cursor.next());
    }
    _cursor.close();
    return _results;
}

From source file:net.ymate.platform.persistence.mongodb.support.DefaultMongoQuery.java

License:Apache License

public IMongoQuery<T> executeQuery(int pageNumber, int pageSize) throws OperatorException {
    if (!__isExecuted) {
        List<T> _results = new ArrayList<T>();
        Long _recordCount = 0L;// w  w w  .  j a va2s .  c  o m
        if (__orderBy != null || pageNumber != 0 || pageSize != 0) {
            DBCursor _cursor = __collection.find(this.__condition, __customFields);
            if (__orderBy != null) {
                _cursor.sort(__orderBy);
            }
            if (pageNumber > 0 && pageSize > 0) {
                _cursor.skip((pageNumber - 1) * pageSize).limit(pageSize);
                _recordCount = executeCount();
            }
            while (_cursor.hasNext()) {
                DBObject _obj = _cursor.next();
                _results.add(__handler.handle(_obj));
            }
        } else {
            DBObject _obj = __collection.findOne(this.__condition, __customFields);
            if (_obj != null) {
                _results.add(__handler.handle(_obj));
            }
        }
        __isExecuted = true;
        this.__resultSet = new PageResultSet<T>(_results, pageNumber, pageSize, _recordCount.intValue());
    }
    return this;
}

From source file:net.ymate.platform.persistence.mongodb.support.MongoDBHelper.java

License:Apache License

public Iterable<DBObject> mapReduce(String collectionName, String map, String reduce, String outputTarget,
        OutputType type, OrderBy order, int pageNumber, int pageSize, DBObject query) throws OperatorException {
    MapReduceOutput _output = getCollection(collectionName).mapReduce(map, reduce, outputTarget, type, query);
    CommandResult _result = _output.getCommandResult();
    if (!_result.ok()) {
        throw new OperatorException(_result.getErrorMessage());
    }//from w w w.  j a v a2s.c  o  m
    DBCollection _collection = _output.getOutputCollection();
    DBCursor _cursor = null;
    if (order != null) {
        _cursor = _collection.find().sort(order.toDBObject());
    } else {
        _cursor = _collection.find();
    }
    if (pageNumber > 0 && pageSize > 0) {
        _cursor.skip((pageNumber - 1) * pageSize).limit(pageSize);
    }
    List<DBObject> _results = new ArrayList<DBObject>();
    for (Iterator<DBObject> _it = _cursor.iterator(); _it.hasNext();) {
        _results.add(_it.next());
    }
    return _results;
}

From source file:org.alfresco.bm.event.mongo.MongoResultService.java

License:Open Source License

@Override
public List<EventRecord> getResults(String eventName, int skip, int limit) {
    DBObject queryObj = QueryBuilder.start().get();
    if (eventName != null) {
        queryObj.put(EventRecord.FIELD_EVENT_NAME, eventName);
    }/*  ww w  .j a  v a2 s  . c  o  m*/
    DBObject sortObj = BasicDBObjectBuilder.start().add(EventRecord.FIELD_START_TIME, Integer.valueOf(1)).get();

    DBCursor cursor = collection.find(queryObj);
    cursor.sort(sortObj);
    cursor.skip(skip);
    cursor.limit(limit);

    // Get all the results and convert them
    int size = cursor.size();
    List<EventRecord> results = new ArrayList<EventRecord>(size);
    try {
        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            EventRecord eventRecord = convertToEventRecord(obj);
            results.add(eventRecord);
        }
    } finally {
        cursor.close();
    }
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug(
                "\n" + "Found results: \n" + "   Query:          " + queryObj + "\n" + "   Skip:           "
                        + skip + "\n" + "   Limit:          " + limit + "\n" + "   Results:        " + size);
    }
    return results;
}

From source file:org.alfresco.bm.event.mongo.MongoResultService.java

License:Open Source License

@Override
public List<EventRecord> getResults(long startTime, long endTime, boolean chartOnly, int skip, int limit) {
    QueryBuilder queryBuilder = QueryBuilder.start().and(EventRecord.FIELD_START_TIME)
            .greaterThanEquals(new Date(startTime)).and(EventRecord.FIELD_START_TIME)
            .lessThan(new Date(endTime));
    if (chartOnly) {
        queryBuilder.and(EventRecord.FIELD_CHART).is(true);
    }/*from  w  w  w  .j av  a2 s.c  o m*/
    DBObject queryObj = queryBuilder.get();
    DBObject sortObj = BasicDBObjectBuilder.start().add(EventRecord.FIELD_START_TIME, Integer.valueOf(1)).get();

    DBCursor cursor = collection.find(queryObj);
    cursor.sort(sortObj);
    cursor.skip(skip);
    cursor.limit(limit);

    // Get all the results and convert them
    int size = cursor.size();
    List<EventRecord> results = new ArrayList<EventRecord>(size);
    try {
        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            EventRecord eventRecord = convertToEventRecord(obj);
            results.add(eventRecord);
        }
    } finally {
        cursor.close();
    }
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug(
                "\n" + "Found results: \n" + "   Query:          " + queryObj + "\n" + "   Skip:           "
                        + skip + "\n" + "   Limit:          " + limit + "\n" + "   Results:        " + size);
    }
    return results;
}

From source file:org.alfresco.bm.event.mongo.MongoResultService.java

License:Open Source License

@Override
public List<EventDetails> getEventDetails(EventResultFilter filter, String filterEventName, int skip,
        int limit) {
    QueryBuilder queryBuilder = QueryBuilder.start();

    // apply filter
    switch (filter) {
    case Failed:/*  w  ww .j ava  2  s.co m*/
        queryBuilder.and(EventRecord.FIELD_SUCCESS).is(false);
        break;

    case Success:
        queryBuilder.and(EventRecord.FIELD_SUCCESS).is(true);
        break;
    default:
        break;
    }

    //apply event name filter
    if (null != filterEventName && !filterEventName.isEmpty()) {
        queryBuilder.and(EventRecord.FIELD_EVENT_NAME).is(filterEventName);
    }

    DBObject queryObj = queryBuilder.get();
    // sort descending to get the newest values first
    DBObject sortObj = BasicDBObjectBuilder.start().add(EventRecord.FIELD_START_TIME, Integer.valueOf(-1))
            .get();
    DBCursor cursor = collection.find(queryObj);
    cursor.sort(sortObj);
    cursor.skip(skip);
    cursor.limit(limit);

    // Get all the results and convert them
    int size = cursor.size();
    List<EventDetails> results = new ArrayList<EventDetails>(size);
    try {
        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            EventDetails eventDetails = convertToEventDetails(obj);
            results.add(eventDetails);
        }
    } finally {
        cursor.close();
    }

    return results;
}

From source file:org.apache.camel.component.mongodb.MongoDbProducer.java

License:Apache License

protected void doFindAll(Exchange exchange) throws Exception {
    DBCollection dbCol = calculateCollection(exchange);
    // do not use getMandatoryBody, because if the body is empty we want to retrieve all objects in the collection
    DBObject query = null;//  ww  w .  j  a  va2 s .  com
    // do not run around looking for a type converter unless there is a need for it
    if (exchange.getIn().getBody() != null) {
        query = exchange.getIn().getBody(DBObject.class);
    }
    DBObject fieldFilter = exchange.getIn().getHeader(MongoDbConstants.FIELDS_FILTER, DBObject.class);

    // get the batch size and number to skip
    Integer batchSize = exchange.getIn().getHeader(MongoDbConstants.BATCH_SIZE, Integer.class);
    Integer numToSkip = exchange.getIn().getHeader(MongoDbConstants.NUM_TO_SKIP, Integer.class);
    Integer limit = exchange.getIn().getHeader(MongoDbConstants.LIMIT, Integer.class);
    DBObject sortBy = exchange.getIn().getHeader(MongoDbConstants.SORT_BY, DBObject.class);
    DBCursor ret = null;
    try {
        if (query == null && fieldFilter == null) {
            ret = dbCol.find(new BasicDBObject());
        } else if (fieldFilter == null) {
            ret = dbCol.find(query);
        } else {
            ret = dbCol.find(query, fieldFilter);
        }

        if (sortBy != null) {
            ret.sort(sortBy);
        }

        if (batchSize != null) {
            ret.batchSize(batchSize.intValue());
        }

        if (numToSkip != null) {
            ret.skip(numToSkip.intValue());
        }

        if (limit != null) {
            ret.limit(limit.intValue());
        }

        Message resultMessage = prepareResponseMessage(exchange, MongoDbOperation.findAll);
        resultMessage.setBody(ret.toArray());
        resultMessage.setHeader(MongoDbConstants.RESULT_TOTAL_SIZE, ret.count());
        resultMessage.setHeader(MongoDbConstants.RESULT_PAGE_SIZE, ret.size());

    } catch (Exception e) {
        // rethrow the exception
        throw e;
    } finally {
        // make sure the cursor is closed
        if (ret != null) {
            ret.close();
        }
    }

}

From source file:org.apache.metamodel.mongodb.mongo2.MongoDbDataContext.java

License:Apache License

private DBCursor getCursor(Table table, List<FilterItem> whereItems, int firstRow, int maxRows) {
    final DBCollection collection = _mongoDb.getCollection(table.getName());

    final DBObject query = createMongoDbQuery(table, whereItems);

    logger.info("Executing MongoDB 'find' query: {}", query);
    DBCursor cursor = collection.find(query);

    if (maxRows > 0) {
        cursor = cursor.limit(maxRows);/*from   w w  w.ja v  a 2s  .  co  m*/
    }
    if (firstRow > 1) {
        final int skip = firstRow - 1;
        cursor = cursor.skip(skip);
    }
    return cursor;
}

From source file:org.apache.metamodel.mongodb.MongoDbDataContext.java

License:Apache License

private DataSet materializeMainSchemaTableInternal(Table table, Column[] columns, List<FilterItem> whereItems,
        int firstRow, int maxRows, boolean queryPostProcessed) {
    final DBCollection collection = _mongoDb.getCollection(table.getName());

    final DBObject query = createMongoDbQuery(table, whereItems);

    logger.info("Executing MongoDB 'find' query: {}", query);
    DBCursor cursor = collection.find(query);

    if (maxRows > 0) {
        cursor = cursor.limit(maxRows);/* ww w . ja v  a2 s.com*/
    }
    if (firstRow > 1) {
        final int skip = firstRow - 1;
        cursor = cursor.skip(skip);
    }

    return new MongoDbDataSet(cursor, columns, queryPostProcessed);
}