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:com.imaginea.mongodb.services.DocumentServiceImpl.java

License:Apache License

/**
 * Gets the list of documents inside a collection in a database in mongo to
 * which user is connected to.// ww w  .  j  av  a2 s . co m
 * 
 * @param dbName
 *            Name of Database
 * @param collectionName
 *            Name of Collection from which to get all Documents
 * 
 * @param query
 *            query to be performed. In case of empty query {} return all
 *            docs.
 * 
 * @param keys
 *            Keys to be present in the resulted docs.
 * 
 * @param limit
 *            Number of docs to show.
 * 
 * @param skip
 *            Docs to skip from the front.
 * 
 * @return List of all documents.
 * @exception EmptyDatabaseNameException
 *                If database name is null
 * @exception EmptyCollectionNameException
 *                If Collection name is null
 * @exception UndefinedDatabaseException
 *                If database is not present
 * @exception UndefinedCollectionException
 *                If Collection is not present
 * @exception DatabaseException
 *                throw super type of UndefinedDatabaseException
 * @exception ValidationException
 *                throw super type of
 *                EmptyDatabaseNameException,EmptyCollectionNameException
 * @exception CollectionException
 *                throw super type of UndefinedCollectionException
 * @exception DocumentException
 *                exception while performing get doc list
 * 
 */

public ArrayList<DBObject> getQueriedDocsList(String dbName, String collectionName, DBObject query,
        DBObject keys, int limit, int skip)
        throws DatabaseException, CollectionException, DocumentException, ValidationException {

    mongoInstance = mongoInstanceProvider.getMongoInstance();

    if (dbName == null) {
        throw new EmptyDatabaseNameException("Database name is null");

    }
    if (dbName.equals("")) {
        throw new EmptyDatabaseNameException("Database Name Empty");
    }

    if (collectionName == null) {
        throw new EmptyCollectionNameException("Collection name is null");
    }
    if (collectionName.equals("")) {
        throw new EmptyCollectionNameException("Collection Name Empty");
    }

    ArrayList<DBObject> dataList = new ArrayList<DBObject>();
    try {
        if (!mongoInstance.getDatabaseNames().contains(dbName)) {
            throw new UndefinedDatabaseException("DB with name [" + dbName + "]DOES_NOT_EXIST");
        }

        if (!mongoInstance.getDB(dbName).getCollectionNames().contains(collectionName)) {
            throw new UndefinedCollectionException("Collection with name [" + collectionName
                    + "] DOES NOT EXIST in Database [" + dbName + "]");
        }
        if (keys.keySet().isEmpty()) {
            keys.put("_id", 1); // For empty keys return all _id of all docs
        }

        // Return Queried Documents
        DBCursor cursor = mongoInstance.getDB(dbName).getCollection(collectionName).find(query, keys);
        cursor.limit(limit);
        cursor.skip(skip);

        if (cursor.hasNext()) {
            while (cursor.hasNext()) {
                dataList.add(cursor.next());
            }
        }
    } catch (MongoException e) {
        throw new DocumentException(ErrorCodes.GET_DOCUMENT_LIST_EXCEPTION, "GET_DOCUMENT_LIST_EXCEPTION",
                e.getCause());
    }
    return dataList;

}

From source file:com.impetus.client.mongodb.query.gfs.KunderaGridFS.java

License:Apache License

/**
 * Finds a list of files matching the given query.
 * // w w w  .j  a  va 2s  . c  o m
 * @param query
 *            the filter to apply
 * @param sort
 *            the fields to sort with
 * @param firstResult
 *            number of files to skip
 * @param maxResult
 *            number of files to return
 * @return list of gridfs files
 */
public List<GridFSDBFile> find(final DBObject query, final DBObject sort, final int firstResult,
        final int maxResult) {
    List<GridFSDBFile> files = new ArrayList<GridFSDBFile>();

    DBCursor c = null;
    try {
        c = _filesCollection.find(query);
        if (sort != null) {
            c.sort(sort);
        }
        c.skip(firstResult).limit(maxResult);
        while (c.hasNext()) {
            files.add(_fix(c.next()));
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return files;
}

From source file:com.janeluo.jfinalplus.plugin.monogodb.MongoKit.java

License:Apache License

private static void page(int pageNumber, int pageSize, DBCursor dbCursor) {
    dbCursor = dbCursor.skip((pageNumber - 1) * pageSize).limit(pageSize);
}

From source file:com.liferay.mongodb.hook.service.impl.MongoExpandoValueLocalServiceImpl.java

License:Open Source License

@Override
public List<ExpandoValue> getColumnValues(long companyId, long classNameId, String tableName, String columnName,
        String data, int start, int end) {

    try {//from   www.ja va  2  s.co  m
        ExpandoColumn expandoColumn = ExpandoColumnLocalServiceUtil.getColumn(companyId, classNameId, tableName,
                columnName);

        DBCollection dbCollection = MongoDBUtil.getCollection(companyId, classNameId, tableName);

        DBCursor dbCursor = null;

        if (Validator.isNotNull(data)) {
            DBObject queryDBObject = new BasicDBObject();

            ExpandoValue expandoValue = ExpandoValueUtil.create(0);

            expandoValue.setColumnId(expandoColumn.getColumnId());
            expandoValue.setData(data);

            queryDBObject.put(columnName, getData(expandoColumn, expandoValue));

            dbCursor = dbCollection.find(queryDBObject);
        } else {
            dbCursor = dbCollection.find();
        }

        if ((start != QueryUtil.ALL_POS) && (end != QueryUtil.ALL_POS)) {
            dbCursor = dbCursor.skip(start).limit(end - start);
        }

        List<ExpandoValue> expandoValues = new ArrayList<ExpandoValue>();

        for (DBObject dbObject : dbCursor.toArray()) {
            BasicDBObject expandoValueDBObject = (BasicDBObject) dbObject;

            ExpandoValue expandoValue = toExpandoValue(expandoValueDBObject, expandoColumn);

            expandoValues.add(expandoValue);
        }

        return expandoValues;
    } catch (PortalException pe) {
        throw new SystemException(pe);
    }
}

From source file:com.mobileman.kuravis.core.services.entity.impl.AbstractEntityServiceImpl.java

License:Apache License

@Override
public List<DBObject> findAll(String entityName, String projection, Pageable page) {
    if (page == null) {
        page = new PageRequest(0, Integer.MAX_VALUE);
    }/*from  www .  j av a2 s  . c  o  m*/

    DBCursor cursor = null;
    if (StringUtils.isEmpty(projection)) {
        cursor = getCollection(entityName).find(new BasicDBObject());
    } else {
        String[] properties = projection.split(",");
        BasicDBObjectBuilder projectionBuilder = BasicDBObjectBuilder.start();
        boolean idWanted = false;
        for (String property : properties) {
            property = property.trim();
            if (!StringUtils.isEmpty(property)) {
                if (property.equals(EntityUtils.ID)) {
                    idWanted = true;
                }

                projectionBuilder.add(property.trim(), true);
            }
        }

        if (idWanted == false) {
            projectionBuilder.append("_id", false);
        }

        cursor = getCollection(entityName).find(new BasicDBObject(), projectionBuilder.get())
                .sort(new BasicDBObject(projection, 1));
    }

    if (page.getSort() != null) {
        cursor = cursor.sort(createSort(page));
    } else if (projection != null) {
        cursor = cursor.sort(new BasicDBObject(projection, 1));
    }

    cursor = cursor.skip(page.getOffset()).limit(page.getPageSize());

    List<DBObject> result = cursor.toArray();
    return result;
}

From source file:com.mobileman.kuravis.core.services.entity.impl.AbstractEntityServiceImpl.java

License:Apache License

/**
 * {@inheritDoc}//from   ww w. ja va2  s  .c  o  m
 * 
 */
@Override
public List<DBObject> findAllByQuery(DBObject query, String projection, Pageable page) {
    DBCursor cursor = null;
    if (StringUtils.isEmpty(projection)) {
        cursor = getCollection().find(query);
    } else {
        cursor = getCollection().find(query, new BasicDBObject(projection, true).append("_id", false));
    }

    if (page.getSort() != null) {
        cursor = cursor.sort(createSort(page));
    } else if (projection != null) {
        cursor = cursor.sort(new BasicDBObject(projection, 1));
    }

    cursor = cursor.skip(page.getOffset()).limit(page.getPageSize());

    List<DBObject> result = cursor.toArray();
    return result;
}

From source file:com.mysema.query.mongodb.MongodbQuery.java

License:Apache License

protected DBCursor createCursor(DBCollection collection, @Nullable Predicate where, QueryModifiers modifiers,
        List<OrderSpecifier<?>> orderBy) {
    DBCursor cursor = collection.find(createQuery(where));
    if (modifiers.getLimit() != null) {
        cursor.limit(modifiers.getLimit().intValue());
    }/*from  w  ww . jav a2  s. com*/
    if (modifiers.getOffset() != null) {
        cursor.skip(modifiers.getOffset().intValue());
    }
    if (orderBy.size() > 0) {
        cursor.sort(serializer.toSort(orderBy));
    }
    return cursor;
}

From source file:com.querydsl.mongodb.AbstractMongodbQuery.java

License:Apache License

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);/*from  ww  w .ja  v a2s .  co m*/
    }
    if (offset != null) {
        cursor.skip(offset);
    }
    if (orderBy.size() > 0) {
        cursor.sort(serializer.toSort(orderBy));
    }
    if (readPreference != null) {
        cursor.setReadPreference(readPreference);
    }
    return cursor;
}

From source file:com.querydsl.mongodb.MongodbQuery.java

License:Apache License

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.intValue());//from ww  w  . j  a  v  a  2  s  .  c  om
    }
    if (offset != null) {
        cursor.skip(offset.intValue());
    }
    if (orderBy.size() > 0) {
        cursor.sort(serializer.toSort(orderBy));
    }
    if (readPreference != null) {
        cursor.setReadPreference(readPreference);
    }
    return cursor;
}

From source file:com.redhat.lightblue.crud.mongo.BasicDocFinder.java

License:Open Source License

@Override
public long find(CRUDOperationContext ctx, DBCollection coll, DBObject mongoQuery, DBObject mongoSort,
        Long from, Long to) {
    LOGGER.debug("Submitting query");
    DBCursor cursor = new FindCommand(null, coll, mongoQuery, null).execute();
    LOGGER.debug("Query evaluated");
    if (mongoSort != null) {
        cursor = cursor.sort(mongoSort);
        LOGGER.debug("Result set sorted");
    }/*from w ww .j  a va 2s . co  m*/
    long ret = cursor.size();
    LOGGER.debug("Applying limits: {} - {}", from, to);
    if (from != null) {
        cursor.skip(from.intValue());
    }
    if (to != null) {
        cursor.limit(to.intValue() - (from == null ? 0 : from.intValue()) + 1);
    }
    LOGGER.debug("Retrieving results");
    List<DBObject> mongoResults = cursor.toArray();
    LOGGER.debug("Retrieved {} results", mongoResults.size());
    List<JsonDoc> jsonDocs = translator.toJson(mongoResults);
    ctx.addDocuments(jsonDocs);
    for (DocCtx doc : ctx.getDocuments()) {
        doc.setOperationPerformed(Operation.FIND);
    }
    LOGGER.debug("Translated DBObjects to json");
    return ret;
}