Example usage for com.mongodb DBCursor limit

List of usage examples for com.mongodb DBCursor limit

Introduction

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

Prototype

public DBCursor limit(final int limit) 

Source Link

Document

Limits the number of elements returned.

Usage

From source file:fr.gouv.vitam.cases.DbRequest.java

License:Open Source License

/**
 * In MongoDB : <br/> /*from   w  w  w  .  j a  v a  2 s  .c om*/
 * find(Query, Projection).sort(SortFilter).skip(SkipFilter).limit(LimitFilter);<br/>
 * In addition, one shall limit the scan by: <br/>
 * find(Query, Projection)._addSpecial( "$maxscan", highlimit)
 *   .sort(SortFilter).skip(SkipFilter).limit(LimitFilter);
 * 
 * @param query
 * @param result to be filtered using query
 * @return the new result or null if the same
 */
private ResultInterface lastFilter(final AbstractQueryParser query, final ResultInterface result) {
    if (simulate) {
        return null;
    }
    boolean filter = (query.getLimit() > 0 || query.getOffset() > 0);
    if (!filter) {
        return null;
    }
    ObjectNode orderBy = query.getOrderBy();
    if (GlobalDatas.PRINT_REQUEST) {
        LOGGER.warn("Req1LevelMD Filter on: Limit {} Offset {} OrderBy {}", query.getLimit(), query.getOffset(),
                orderBy);
    }
    final ResultInterface subresult = CassandraAccess.createOneResult();
    BasicDBObject inClause = getInClauseForField(DAip.ID, result.getCurrentDaip());
    final DBCursor cursor = mdAccess.daips.collection.find(inClause, ID_NBCHILD);
    if (query.getLimit() > 0) {
        cursor.limit(query.getLimit());
    }
    if (query.getOffset() > 0) {
        cursor.skip(query.getOffset());
    }
    if (orderBy != null) {
        // orderBy is used only if limit and/or offset is set
        cursor.sort((DBObject) orderBy);
    }
    long tempCount = 0;
    while (cursor.hasNext()) {
        final DAip maip = (DAip) cursor.next();
        final String mid = maip.getId();
        subresult.getCurrentDaip().add(mid);
        tempCount += maip.getLong(DAip.NBCHILD);
    }
    cursor.close();
    if (subresult.getCurrentDaip().containsAll(result.getCurrentDaip())) {
        // same so don't change it
        return null;
    }
    subresult.setNbSubNodes(tempCount);
    // Not updateMinMax since result is not "valid" path but node UUID and not needed
    subresult.setMinLevel(result.getMinLevel());
    subresult.setMaxLevel(result.getMaxLevel());
    subresult.setLoaded(true);
    if (GlobalDatas.PRINT_REQUEST) {
        subresult.putBeforeSave();
        LOGGER.warn("Filtered: {}", subresult);
    }
    return subresult;
}

From source file:fr.gouv.vitam.mdbes.DbRequest.java

License:Open Source License

/**
 * In MongoDB : <br/> /*from w  w w.  ja v  a  2  s. c o  m*/
 * find(Query, Projection).sort(SortFilter).skip(SkipFilter).limit(LimitFilter);<br/>
 * In addition, one shall limit the scan by: <br/>
 * find(Query, Projection)._addSpecial( "$maxscan", highlimit)
 *   .sort(SortFilter).skip(SkipFilter).limit(LimitFilter);
 * 
 * @param query
 * @param result to be filtered using query
 * @return the new result or null if the same
 */
private ResultInterface lastFilter(final AbstractQueryParser query, final ResultInterface result) {
    if (simulate) {
        return null;
    }
    boolean filter = (query.getLimit() > 0 || query.getOffset() > 0);
    if (!filter) {
        return null;
    }
    ObjectNode orderBy = query.getOrderBy();
    if (GlobalDatas.PRINT_REQUEST) {
        LOGGER.warn("Req1LevelMD Filter on: Limit {} Offset {} OrderBy {}", query.getLimit(), query.getOffset(),
                orderBy);
    }
    final ResultInterface subresult = MongoDbAccess.createOneResult();
    BasicDBObject inClause = getInClauseForField(DAip.ID, result.getCurrentDaip());
    final DBCursor cursor = mdAccess.daips.collection.find(inClause, ID_NBCHILD);
    if (query.getLimit() > 0) {
        cursor.limit(query.getLimit());
    }
    if (query.getOffset() > 0) {
        cursor.skip(query.getOffset());
    }
    if (orderBy != null) {
        // orderBy is used only if limit and/or offset is set
        cursor.sort((DBObject) orderBy);
    }
    long tempCount = 0;
    while (cursor.hasNext()) {
        final DAip maip = (DAip) cursor.next();
        final String mid = maip.getId();
        subresult.getCurrentDaip().add(mid);
        tempCount += maip.getLong(DAip.NBCHILD);
    }
    cursor.close();
    if (subresult.getCurrentDaip().containsAll(result.getCurrentDaip())) {
        // same so don't change it
        return null;
    }
    subresult.setNbSubNodes(tempCount);
    // Not updateMinMax since result is not "valid" path but node UUID and not needed
    subresult.setMinLevel(result.getMinLevel());
    subresult.setMaxLevel(result.getMaxLevel());
    subresult.setLoaded(true);
    if (GlobalDatas.PRINT_REQUEST) {
        subresult.putBeforeSave();
        LOGGER.warn("Filtered: {}", subresult);
    }
    return subresult;
}

From source file:GeoHazardServices.Inst.java

License:Apache License

@POST
@Path("/fetch")
@Produces(MediaType.APPLICATION_JSON)/*from w ww .j a v a 2 s. c o  m*/
public String fetch(@Context HttpServletRequest request, @FormParam("limit") @DefaultValue("0") int limit,
        @FormParam("delay") @DefaultValue("0") int delay,
        @FormParam("undersea") @DefaultValue("false") boolean undersea,
        @CookieParam("server_cookie") String session) {

    /* check session key and find out if the request comes from an authorized user */
    User user = signedIn(session); /* returns null if user is not logged in */

    /* create lists for general and user specific earthquake entries */
    ArrayList<DBObject> mlist = new ArrayList<DBObject>();
    ArrayList<DBObject> ulist = new ArrayList<DBObject>();

    /* we want all entries since the beginning of time */
    Date maxTimestamp = new Date();

    /* used to convert to desired time format used by MongoDB */
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

    /* select collection which contain the earthquake entries */
    DBCollection coll = db.getCollection("eqs");

    //ArrayList<User> users = new ArrayList<User>( institutions.values() );
    ArrayList<User> users = new ArrayList<User>();
    users.add(user);

    if (user != null) {

        if (user.inst != null) {
            users.add(institutions.get(user.inst));
        } else {
            users.add(institutions.get("gfz"));
        }

        DBCursor csr = db.getCollection("users").find(
                new BasicDBObject("username", user.name).append("provider", new BasicDBObject("$ne", null)));
        if (csr.hasNext()) {
            for (Object p : (BasicDBList) csr.next().get("provider")) {
                DBObject inst = db.getCollection("institutions").findOne(new BasicDBObject("_id", p));
                if (inst != null)
                    users.add(institutions.get(inst.get("name")));
            }
        }
    }

    /* return only entries that are older than 'delay' minutes */
    Date upperTimeLimit = new Date(System.currentTimeMillis() - delay * 60 * 1000);

    /* get earthquakes for each of the given users */
    for (User curUser : users) {

        if (curUser == null)
            continue;

        /* create DB query */
        BasicDBObject inQuery = new BasicDBObject("user", curUser.objId);

        if (undersea)
            inQuery.append("prop.sea_area", new BasicDBObject("$ne", null));

        if (delay > 0)
            inQuery.append("prop.date", new BasicDBObject("$lt", upperTimeLimit));

        inQuery.append("depr", new BasicDBObject("$ne", true));
        inQuery.append("evtset", null);

        /* query DB, sort the results by date and limit the number of returned entries */
        DBCursor cursor = coll.find(inQuery).sort(new BasicDBObject("prop.date", -1));

        if (limit > 0)
            cursor = cursor.limit(limit);

        /* walk through the returned entries */
        for (DBObject obj : cursor) {

            obj.removeField("image");

            /* check if entry belongs to general or user specific list */
            if (user != null && obj.get("user").equals(user.objId)) {

                ulist.add(obj);
            } else {
                mlist.add(obj);
            }

            /* update timestamp */
            Date timestamp = (Date) obj.get("timestamp");
            if (timestamp.after(maxTimestamp)) {
                maxTimestamp = timestamp;
            }
        }

        /* clean up query */
        cursor.close();
    }

    /* create new JSON object that can be used directly within JavaScript */
    JsonObject jsonObj = new JsonObject();
    jsonObj.add("main", gson.toJsonTree(mlist));
    jsonObj.add("user", gson.toJsonTree(ulist));

    if (user != null) {

        List<DBObject> msglist = msg(limit, user);

        if (!msglist.isEmpty()) {
            Date timestamp = (Date) msglist.get(0).get("CreatedTime");
            if (timestamp.after(maxTimestamp)) {
                maxTimestamp = timestamp;
            }
        }

        jsonObj.add("msg", gson.toJsonTree(msglist));

    } else {

        jsonObj.add("msg", gson.toJsonTree(new ArrayList<DBObject>()));
    }

    List<DBObject> evtsets = new ArrayList<DBObject>();
    if (user != null) {
        BasicDBObject query = new BasicDBObject("user", user.objId);
        query.append("timestamp", new BasicDBObject("$lte", maxTimestamp));
        DBCursor cursor = db.getCollection("evtsets").find(query).sort(new BasicDBObject("timestamp", -1))
                .limit(100);
        evtsets = cursor.toArray();
    }
    jsonObj.add("evtsets", gson.toJsonTree(evtsets));

    /* TODO */
    if (user != null) {
        for (Map.Entry<String, IDataProvider> entry : providers.entrySet()) {
            List<DBObject> list = entry.getValue().fetch(user, maxTimestamp, limit);
            jsonObj.add(entry.getKey(), gson.toJsonTree(list));
        }
    }

    jsonObj.addProperty("ts", sdf.format(maxTimestamp));
    return jsonObj.toString();
}

From source file:GeoHazardServices.Inst.java

License:Apache License

private List<DBObject> msg(int limit, User user) {

    if (user == null)
        return null;

    DBCollection coll = db.getCollection("messages_sent");

    BasicDBObject inQuery = new BasicDBObject("SenderID", user.objId);

    /* query DB, sort the results by date and limit the number of returned entries */
    DBCursor cursor = coll.find(inQuery).sort(new BasicDBObject("CreatedTime", -1));

    if (limit > 0)
        cursor = cursor.limit(limit);

    List<DBObject> result = cursor.toArray();
    cursor.close();/*from  w w w .  j a  v a2  s .  co m*/

    inQuery = new BasicDBObject("ReceiverID", user.objId);
    coll = db.getCollection("messages_received");
    cursor = coll.find(inQuery).sort(new BasicDBObject("CreatedTime", -1));

    if (limit > 0)
        cursor = cursor.limit(limit);

    for (DBObject obj : cursor) {

        DBCursor csrUser = db.getCollection("users").find(new BasicDBObject("_id", obj.get("SenderID")));

        if (csrUser.hasNext())
            obj.put("From", (String) csrUser.next().get("username"));

        obj.put("To", new String[] { user.name });
        obj.put("Dir", "in");

        result.add(obj);
    }

    cursor.close();

    Collections.sort(result, new DateComparator("CreatedTime", -1));

    /* add parent event as sub-object because we want to show it on click */
    for (DBObject msg : result) {

        DBCursor csr = db.getCollection("eqs").find(new BasicDBObject("_id", msg.get("ParentId")));

        if (csr.hasNext()) {
            DBObject obj = csr.next();
            obj.removeField("image");
            msg.put("parentEvt", obj);
        }
    }

    return result;
}

From source file:hulop.hokoukukan.utils.MongoAdapter.java

License:Open Source License

@Override
public JSONArray getLogs(String clientId, String start, String end, String skip, String limit, String event) {
    BasicDBObject query = new BasicDBObject();
    if (clientId != null) {
        query.append("client", clientId);
    }// ww w.  j  a v  a  2 s .  com

    new BasicDBObject("Date", new BasicDBObject("$gt", start).append("$lte", end));

    BasicDBObject timeRange = new BasicDBObject();
    if (start != null) {
        timeRange.append("$gte", Long.parseLong(start));
    }
    if (end != null) {
        timeRange.append("$lt", Long.parseLong(end));
    }
    if (timeRange.size() > 0) {
        query.append("timestamp", timeRange);
    }
    if (event != null) {
        query.append("event", event);
    }
    System.out.println(query.toString());
    DBCursor cursor = logCol.find(query);
    if (skip != null) {
        cursor = cursor.skip(Integer.parseInt(skip));
    }
    if (limit != null) {
        cursor = cursor.limit(Integer.parseInt(limit));
    }
    JSONArray result = new JSONArray();
    try {
        while (cursor.hasNext()) {
            result.add(new JSONObject(cursor.next().toString()));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:in.mtap.iincube.mongoapi.MongoReader.java

License:Apache License

private DBCursor getCursor() {
    DBCollection collection = collectionFactory.get();
    assertNotNull(queryObject, "findQuery == null");
    DBCursor cursor;
    if (fields != null) {
        BasicDBObject selectFields = new BasicDBObject();
        for (String field : fields) {
            selectFields.append(field, 1);
        }/*from ww  w  . ja  va 2 s . c o m*/
        cursor = collection.find(queryObject, selectFields);
    } else {
        cursor = collection.find(queryObject);
    }
    if (skip > 0)
        cursor.skip(skip);
    if (limit > 0)
        cursor.limit(limit);
    if (sortObject != null)
        cursor.sort(sortObject);
    return cursor;
}

From source file:io.liveoak.mongo.MongoCollectionResource.java

License:Open Source License

@Override
public Collection<Resource> members(RequestContext ctx) throws Exception {

    LinkedList<Resource> members = new LinkedList<>();

    DBObject returnFields = new BasicDBObject();
    if (ctx.returnFields() != null && !ctx.returnFields().child(LiveOak.MEMBERS).isEmpty()) {
        ReturnFields membersReturnFields = ctx.returnFields().child(LiveOak.MEMBERS);
        if (!membersReturnFields.isAll()) {
            membersReturnFields.forEach((fieldName) -> {
                returnFields.put(fieldName, true);
            });// w  ww .  j  a  v a2s . com
        }
    }

    DBCursor dbCursor = dbCollection.find(queryObject, returnFields);

    ResourceParams resourceParams = ctx.resourceParams();
    if (resourceParams != null && resourceParams.contains("hint")) {
        String hint = resourceParams.value("hint");
        if (hint.startsWith("{")) {
            try {
                DBObject hintObject = (DBObject) JSON.parse(hint);
                dbCursor.hint(hintObject);
            } catch (Exception e) {
                throw new NotAcceptableException(uri().toString(),
                        "Invalid JSON format for the 'hint' parameter", e);
            }
        } else {
            dbCursor.hint(hint);
        }
    }

    if (explainQuery) {
        members.add(new MongoEmbeddedObjectResource(this, dbCursor.explain()));
    } else {
        Sorting sorting = ctx.sorting();
        if (sorting != null) {
            BasicDBObject sortingObject = new BasicDBObject();
            for (Sorting.Spec spec : sorting) {
                sortingObject.append(spec.name(), spec.ascending() ? 1 : -1);
            }
            dbCursor = dbCursor.sort(sortingObject);
        }

        Pagination pagination = ctx.pagination();
        if (pagination != null) {
            dbCursor.limit(pagination.limit());
            dbCursor.skip(pagination.offset());
        }

        try {
            dbCursor.hasNext();
        } catch (Exception e) {
            throw new ResourceProcessingException(
                    "Exception encountered trying to fetch data from the Mongo Database", e);
        }

        dbCursor.forEach((dbObject) -> {
            members.add(new MongoBaseObjectResource(this, dbObject));
        });
    }

    return members;
}

From source file:jahspotify.storage.statistics.MongoDBHistoricalStorage.java

License:Apache License

@Override
public Collection<TrackHistory> getHistory(final int index, final int count,
        final HistoryCriteria... historyCriterias) {
    final DBCollection tracks = _db.getCollection("history");

    final DBCursor dbObjects = tracks.find();
    dbObjects.skip(index);//from   w w  w  .j ava  2s.  c  o  m
    dbObjects.limit(count);
    final BasicDBObject orderBy = new BasicDBObject();
    orderBy.put("startTime", -1);
    dbObjects.sort(orderBy);

    return new AbstractCollection<TrackHistory>() {
        @Override
        public Iterator<TrackHistory> iterator() {
            return new MongoDBHistoryCursor(dbObjects);
        }

        @Override
        public int size() {
            return dbObjects.size();
        }
    };
}

From source file:me.carbou.mathieu.tictactoe.db.DBCollection.java

License:Apache License

public Stream<Map<String, Object>> find(Map where, Map fields, Map sort, Function<Map, Map> transform,
        int limit, int skip) {
    final DBCursor cursor = getCollection().find(new BasicDBObject(addWhere(where)),
            new BasicDBObject(preFind(fields)));
    if (!sort.isEmpty())
        cursor.sort(new BasicDBObject(sort));
    if (skip > 0)
        cursor.skip(skip);//from w  w w  .j a va  2s. c  om
    if (limit > DB.NO_LIMIT)
        cursor.limit(limit);
    int est = cursor.size();
    Spliterator<Map<String, Object>> spliterator = new Spliterators.AbstractSpliterator<Map<String, Object>>(
            est, NONNULL | ORDERED | SIZED | IMMUTABLE) {
        @Override
        public boolean tryAdvance(Consumer<? super Map<String, Object>> action) {
            if (cursor.hasNext()) {
                action.accept(postFind(where, cursor.next(), transform));
                return true;
            } else {
                cursor.close();
                return false;
            }
        }
    };
    return StreamSupport.stream(spliterator, false);
}

From source file:me.yyam.mongodbutils.MongoDbOperater.java

public List find(QueryInfo queryInfo) {
    DB db = mongoClient.getDB(queryInfo.dbName);
    DBCollection coll = db.getCollection(queryInfo.collName);
    DBCursor cursor = null;
    if (queryInfo.query != null) {
        cursor = coll.find(queryInfo.query);
    } else {//from  ww  w.  j a v a  2 s  .  c om
        cursor = coll.find();
    }
    if (queryInfo.order != null) {
        cursor = cursor.sort(queryInfo.order);
    }
    if (queryInfo.limit != null) {
        cursor.limit(queryInfo.limit.intValue());
    }
    if (queryInfo.skip != null) {
        cursor.skip(queryInfo.skip.intValue());
    }
    return cursor2list(cursor);
}