Example usage for com.mongodb.client FindIterable limit

List of usage examples for com.mongodb.client FindIterable limit

Introduction

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

Prototype

FindIterable<TResult> limit(int limit);

Source Link

Document

Sets the limit to apply.

Usage

From source file:org.gennai.gungnir.topology.processor.MongoFetchProcessor.java

License:Apache License

private List<List<Object>> find(Document execQuery) {
    List<List<Object>> valuesList = Lists.newArrayList();
    FindIterable<Document> find = collection.find(execQuery).projection(fetchFields);
    if (sort != null) {
        find.sort(sort);//from  w  w w  . ja  v  a2 s  .  c  o  m
    }
    if (limit != null) {
        find.limit(limit);
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("Fetch from '{}.{}' query {}", dbName, collectionName, execQuery);
    }

    MongoCursor<Document> cursor = find.iterator();
    try {
        while (cursor.hasNext()) {
            Document doc = cursor.next();

            List<Object> values = Lists.newArrayListWithCapacity(fetchFieldNames.length);
            for (String fieldName : fetchFieldNames) {
                values.add(toValue(doc.get(fieldName)));
            }
            valuesList.add(values);
        }
    } finally {
        cursor.close();
    }
    return valuesList;
}

From source file:org.helm.rest.MongoDB.java

public JSONObject List(String table, String cols, BsonDocument where, BsonDocument sortby, int page,
        int countperpage) {
    if (page < 1)
        page = 1;/*w ww .j  ava  2s . c  o  m*/
    if (countperpage < 1)
        countperpage = 10;

    long count;
    FindIterable iter;
    MongoCollection coll = db.getCollection(table);
    if (where == null) {
        count = coll.count();
        iter = coll.find();
    } else {
        count = coll.count(where);
        iter = coll.find(where);
    }

    if (sortby != null)
        iter = iter.sort(sortby);

    if (cols != null) {
        String[] ss = cols.split(",");
        Document fields = new Document("_id", false);
        for (int i = 0; i < ss.length; ++i) {
            fields.append(ss[i].trim().toLowerCase(), true);
        }
        iter = iter.projection(fields);
    }

    long mod = count % countperpage;
    long pages = (count - mod) / countperpage + (mod == 0 ? 0 : 1);

    if (page > 1)
        iter = iter.skip((page - 1) * countperpage);
    iter = iter.limit(countperpage);
    MongoCursor cur = iter.iterator();

    JSONObject ret = new JSONObject();
    ret.put("page", page);
    ret.put("pages", pages);
    ret.put("rows", ResultSet2Json(cur));

    cur.close();
    return ret;
}

From source file:org.nuxeo.directory.mongodb.MongoDBSession.java

License:Apache License

@Override
public DocumentModelList query(Map<String, Serializable> filter, Set<String> fulltext,
        Map<String, String> orderBy, boolean fetchReferences, int limit, int offset) throws DirectoryException {

    Document bson = buildQuery(filter, fulltext);

    DocumentModelList entries = new DocumentModelListImpl();

    FindIterable<Document> results = getCollection().find(bson).skip(offset);
    if (limit > 0) {
        results.limit(limit);
    }/*from   www .  jav a2  s  . c  om*/
    for (Document resultDoc : results) {

        // Cast object to document model
        Map<String, Object> fieldMap = MongoDBSerializationHelper.bsonToFieldMap(resultDoc);
        DocumentModel doc = fieldMapToDocumentModel(fieldMap);

        if (fetchReferences) {
            Map<String, List<String>> targetIdsMap = new HashMap<>();
            for (Reference reference : directory.getReferences()) {
                List<String> targetIds;
                if (reference instanceof MongoDBReference) {
                    MongoDBReference mongoReference = (MongoDBReference) reference;
                    targetIds = mongoReference.getTargetIdsForSource(doc.getId(), this);
                } else {
                    targetIds = reference.getTargetIdsForSource(doc.getId());
                }
                targetIds = new ArrayList<>(targetIds);
                Collections.sort(targetIds);
                String fieldName = reference.getFieldName();
                targetIdsMap.computeIfAbsent(fieldName, key -> new ArrayList<>()).addAll(targetIds);
            }
            for (Map.Entry<String, List<String>> entry : targetIdsMap.entrySet()) {
                String fieldName = entry.getKey();
                List<String> targetIds = entry.getValue();
                try {
                    doc.setProperty(schemaName, fieldName, targetIds);
                } catch (PropertyException e) {
                    throw new DirectoryException(e);
                }
            }
        }
        entries.add(doc);
    }

    if (orderBy != null && !orderBy.isEmpty()) {
        getDirectory().orderEntries(entries, orderBy);
    }

    return entries;
}

From source file:org.opencb.commons.datastore.mongodb.MongoDBNativeQuery.java

License:Apache License

public FindIterable<Document> find(Bson query, Bson projection, QueryOptions options) {

    if (projection == null) {
        projection = getProjection(projection, options);
    }/*from ww  w  .  j a v  a2 s .  com*/

    FindIterable<Document> findIterable = dbCollection.find(query).projection(projection);

    int limit = (options != null) ? options.getInt(QueryOptions.LIMIT, 0) : 0;
    if (limit > 0) {
        findIterable.limit(limit);
    }

    int skip = (options != null) ? options.getInt(QueryOptions.SKIP, 0) : 0;
    if (skip > 0) {
        findIterable.skip(skip);
    }

    Object sortObject = (options != null) ? options.get(QueryOptions.SORT) : null;
    if (sortObject != null) {
        if (sortObject instanceof Bson) {
            findIterable.sort(((Bson) sortObject));
        } else if (sortObject instanceof String) {
            String order = options.getString(QueryOptions.ORDER, "DESC");
            if (order.equalsIgnoreCase(QueryOptions.ASCENDING) || order.equalsIgnoreCase("ASC")
                    || order.equals("1")) {
                findIterable.sort(Sorts.ascending(((String) sortObject)));
            } else {
                findIterable.sort(Sorts.descending(((String) sortObject)));
            }
        }
    }

    if (options != null && options.containsKey(MongoDBCollection.BATCH_SIZE)) {
        findIterable.batchSize(options.getInt(MongoDBCollection.BATCH_SIZE, 20));
    }

    if (options != null && options.containsKey(QueryOptions.TIMEOUT)) {
        findIterable.maxTime(options.getLong(QueryOptions.TIMEOUT), TimeUnit.MILLISECONDS);
    }

    return findIterable;
}

From source file:org.radarcns.mongo.util.MongoHelper.java

License:Apache License

/**
 * Finds all documents belonging to the given subject, source and project.
 * Close the returned iterator after use, for example with a try-with-resources construct.
 *
 * @param collection MongoDB collection name that will be queried
 * @param project project name/*  www.j av a  2 s . c o  m*/
 * @param subject subject ID
 * @param source source ID
 * @param sortBy Field to sort by. If sortBy is {@code null}, the data will not be sorted.
 *               The field should be prefixed with {@link MongoHelper#KEY} or
 *               {@link MongoHelper#VALUE}.
 * @param order {@code 1} means ascending while {@code -1} means descending
 * @param limit is the number of document that will be retrieved. If the limit is {@code null},
 *              no limit is used.
 * @return a MongoDB cursor containing all documents from query.
 * @throws IllegalArgumentException if sortBy does not start with a key or value object.
 */
public static MongoCursor<Document> findDocumentBySource(MongoCollection<Document> collection, String project,
        String subject, String source, String sortBy, int order, Integer limit) {

    createIndexIfNotAvailable(collection, indexProjectSubjectSource);

    FindIterable<Document> result = collection.find(filterSource(project, subject, source));

    if (sortBy != null) {
        if (!sortBy.startsWith(KEY + ".") && !sortBy.startsWith(VALUE + ".")) {
            throw new IllegalArgumentException(
                    "Should sort by a MongoHelper.KEY or MongoHelper.VALUE property.");
        }
        result = result.sort(new BasicDBObject(sortBy, order));
    }
    if (limit != null) {
        result = result.limit(limit);
    }

    return result.iterator();
}

From source file:rapture.audit.mongodb.MongoDBAuditLog.java

License:Open Source License

@Override
public List<AuditLogEntry> getRecentUserActivity(String user, final int count) {
    final Document sort = getSortObject();
    final Document query = new Document();
    Document test = new Document();
    test.put("$eq", user);
    query.put(USER, test);/*from  w  w  w.  j ava  2  s  .c  o  m*/
    MongoRetryWrapper<List<AuditLogEntry>> wrapper = new MongoRetryWrapper<List<AuditLogEntry>>() {
        public FindIterable<Document> makeCursor() {
            FindIterable<Document> ret = getAuditCollection().find(query).sort(sort);
            if (count > 0) {
                ret = ret.limit(count);
            }
            return ret;
        }

        public List<AuditLogEntry> action(FindIterable<Document> cursor) {
            List<AuditLogEntry> ret = new ArrayList<AuditLogEntry>();
            fillFromCursor(ret, cursor);
            return ret;
        }
    };
    return wrapper.doAction();
}

From source file:rapture.repo.mongodb.MongoDbDataStore.java

License:Open Source License

@Override
public RaptureQueryResult runNativeQuery(final String repoType, final List<String> queryParams) {
    if (repoType.toUpperCase().equals(MONGODB)) {

        MongoRetryWrapper<RaptureQueryResult> wrapper = new MongoRetryWrapper<RaptureQueryResult>() {

            @Override//from ww  w .j  av a2 s. c  o m
            public FindIterable<Document> makeCursor() {
                // Here we go, the queryParams are basically
                // (1) the searchCriteria
                Document queryObj = getQueryObjFromQueryParams(queryParams);
                // Document fieldObj =
                // getFieldObjFromQueryParams(queryParams);
                MongoCollection<Document> collection = MongoDBFactory.getCollection(instanceName, tableName);
                FindIterable<Document> find = collection.find(queryObj).batchSize(100);
                if (queryParams.size() > 2) {
                    Map<String, Object> options = JacksonUtil.getMapFromJson(queryParams.get(2));
                    if (options.containsKey(SKIP)) {
                        find.skip((Integer) options.get(SKIP));
                    }
                    if (options.containsKey(LIMIT)) {
                        find.limit((Integer) options.get(LIMIT));
                    }
                    if (options.containsKey(SORT)) {
                        Map<String, Object> sortInfo = JacksonUtil.getMapFromJson(options.get(SORT).toString());
                        Document sortInfoObject = new Document();
                        sortInfoObject.putAll(sortInfo);
                        find.sort(sortInfoObject);
                    }
                }
                return find;
            }

            @Override
            public RaptureQueryResult action(FindIterable<Document> iterable) {
                RaptureQueryResult res = new RaptureQueryResult();
                for (Document d : iterable) {
                    res.addRowContent(new JsonContent(d.toString()));
                }
                return res;
            }

        };
        return wrapper.doAction();
    } else {
        throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_BAD_REQUEST,
                mongoMsgCatalog.getMessage("Mismatch", repoType));
    }
}

From source file:rapture.table.mongodb.MongoIndexHandler.java

License:Open Source License

@Override
public List<TableRecord> queryTable(final TableQuery querySpec) {

    MongoRetryWrapper<List<TableRecord>> wrapper = new MongoRetryWrapper<List<TableRecord>>() {
        @Override/*from w ww  .  j  a  va  2s .  co  m*/
        public FindIterable<Document> makeCursor() {
            FindIterable<Document> ret;
            MongoCollection<Document> collection = MongoDBFactory.getCollection(instanceName, tableName);

            // Convert the query into that for a mongodb collection, then
            // call find
            Document query = EpochManager.getNotEqualEpochQueryObject();
            if (querySpec.getFieldTests() != null) {
                for (TableSelect sel : querySpec.getFieldTests()) {
                    switch (sel.getOper()) {
                    case "=":
                        query.append(sel.getFieldName(), sel.getTestValue());
                        break;
                    case ">":
                        Document gt = new Document();
                        gt.append("$gt", sel.getTestValue());
                        query.append(sel.getFieldName(), gt);
                        break;
                    case "<":
                        Document lt = new Document();
                        lt.append("$lt", sel.getTestValue());
                        query.append(sel.getFieldName(), lt);
                        break;
                    case "LIKE":
                        query.append(sel.getFieldName(),
                                java.util.regex.Pattern.compile(sel.getTestValue().toString()));
                        break;
                    }
                }
            }

            // That's the query, now determine the return fields...
            List<String> projection = querySpec.getFieldReturns();
            if (projection != null) {
                projection.add(KEY);
            }

            // Now we need to do the query, with a limit and skip applied if
            // necessary
            Document sort = new Document();
            if (querySpec.getSortFields() != null) {
                for (TableColumnSort sortField : querySpec.getSortFields()) {
                    sort.put(sortField.getFieldName(), sortField.getAscending() ? 1 : -1);
                }
            }

            ret = collection.find(query).projection(Projections
                    .include((List<String>) ((projection == null) ? Collections.emptyList() : projection)));

            if (!sort.isEmpty()) {
                ret = ret.sort(sort);
            }
            if (querySpec.getSkip() != 0) {
                ret = ret.skip(querySpec.getSkip());
            }
            if (querySpec.getLimit() != 0) {
                ret = ret.limit(querySpec.getLimit());
            }
            return ret;
        }

        @Override
        public List<TableRecord> action(FindIterable<Document> cursor) {
            List<TableRecord> records = new ArrayList<>();
            if (cursor != null) {
                for (Document obj : cursor) {
                    if (obj != null) {
                        TableRecord rec = new TableRecord();
                        rec.setKeyName(obj.getString(KEY));
                        rec.setFields(obj);
                        rec.setContent(obj.toString());
                        records.add(rec);
                    }
                }
            }
            return records;
        }
    };

    return wrapper.doAction();
}

From source file:rapture.table.mongodb.MongoIndexHandler.java

License:Open Source License

public TableQueryResult query(final IndexQuery indexQuery) {
    if (log.isDebugEnabled()) {
        log.debug("Parsed query " + indexQuery);
    }//from   w w w. ja v  a 2s.c om
    TableQueryResult res = new TableQueryResult();
    final Document mongoQuery = getClause(indexQuery.getWhere());
    final MongoCollection<Document> collection = MongoDBFactory.getCollection(instanceName, tableName);
    List<List<Object>> rows = new ArrayList<>();

    List<String> fieldList = indexQuery.getSelect().getFieldList();
    // Mongo can't do distinct based on multiple fields for some reason

    if (!indexQuery.isDistinct()) {
        // What fields to return
        final Document fields = new Document();
        for (String fieldName : indexQuery.getSelect().getFieldList()) {
            log.debug("Adding return field " + fieldName);
            fields.put(fieldName, 1);
        }
        res.setColumnNames(indexQuery.getSelect().getFieldList());
        fields.put(KEY, 1);

        MongoRetryWrapper<List<List<Object>>> wrapper = new MongoRetryWrapper<List<List<Object>>>() {

            @Override
            public FindIterable<Document> makeCursor() {

                FindIterable<Document> ret;
                if (fields.isEmpty()) {
                    ret = collection.find(mongoQuery);
                } else {
                    fields.put(KEY, 1);
                    ret = collection.find(mongoQuery).projection(fields);
                }

                if (indexQuery.getOrderBy().getFieldList().size() > 0) {
                    Document sort = new Document();
                    for (String field : indexQuery.getOrderBy().getFieldList()) {
                        sort.put(field, indexQuery.getDirection() != OrderDirection.DESC ? 1 : -1);

                    }
                    ret = ret.sort(sort);
                }

                int skip = indexQuery.getSkip();
                if (skip > 0) {
                    ret = ret.skip(skip);
                }

                int limit = indexQuery.getLimit();
                if (limit > 0) {
                    // By specifying a negative limit we tell Mongo that it can close the cursor after returning a single batch.
                    ret = ret.limit(-(limit));
                }

                return ret;
            }

            @Override
            public List<List<Object>> action(FindIterable<Document> cursor) {
                List<List<Object>> rows = new ArrayList<>();
                for (Document obj : cursor) {
                    List<Object> row = new ArrayList<>();
                    for (String field : indexQuery.getSelect().getFieldList()) {
                        row.add(obj.get(field));
                    }
                    rows.add(row);
                }
                return rows;
            }
        };

        res.setRows(wrapper.doAction());
        return res;
        // We are done.

    } else if (fieldList.size() > 1) {
        // What fields to return
        final Document fields = new Document();
        for (String fieldName : indexQuery.getSelect().getFieldList()) {
            log.debug("Adding return field " + fieldName);
            fields.put(fieldName, 1);
        }
        res.setColumnNames(indexQuery.getSelect().getFieldList());
        fields.put(KEY, 1);

        MongoRetryWrapper<List<List<Object>>> wrapper = new MongoRetryWrapper<List<List<Object>>>() {

            @Override
            public FindIterable<Document> makeCursor() {

                FindIterable<Document> ret;
                if (fields.isEmpty()) {
                    ret = collection.find(mongoQuery);
                } else {
                    fields.put(KEY, 1);
                    ret = collection.find(mongoQuery).projection(fields);
                }

                if (indexQuery.getOrderBy().getFieldList().size() > 0) {
                    Document sort = new Document();
                    for (String field : indexQuery.getOrderBy().getFieldList()) {
                        sort.put(field, indexQuery.getDirection() != OrderDirection.DESC ? 1 : -1);

                    }
                    ret = ret.sort(sort);
                }

                // We can't apply SKIP and LIMIT here because we must drop the fields that aren't distinct;
                // Mongo doesn't appear to support distinct on multiple keys
                return ret;
            }

            @Override
            public List<List<Object>> action(FindIterable<Document> cursor) {

                int limit = (indexQuery.getSkip()) + (indexQuery.getLimit());
                if (limit == 0)
                    limit = Integer.MAX_VALUE;

                List<List<Object>> rows = new ArrayList<>();
                for (Document obj : cursor) {
                    List<Object> row = new ArrayList<>();
                    for (String field : indexQuery.getSelect().getFieldList()) {
                        row.add(obj.get(field));
                    }
                    if (indexQuery.isDistinct() && rows.contains(row))
                        continue;
                    rows.add(row);
                    if (rows.size() > limit)
                        break;
                }
                return rows;
            }
        };

        rows = wrapper.doAction();
        // We are not done - still need to apply skip and limit

    } else {
        String key = fieldList.get(0);
        DistinctIterable<String> values = collection.distinct(key, mongoQuery, String.class);
        for (String v : values) {
            rows.add(ImmutableList.of(v));
        }

        res.setColumnNames(ImmutableList.of(key));
        if (indexQuery.getOrderBy().getFieldList().size() > 0) {
            List<String> columnNames = indexQuery.getSelect().getFieldList();
            Collections.sort(rows, RowComparatorFactory.createComparator(indexQuery.getOrderBy().getFieldList(),
                    columnNames, indexQuery.getDirection()));
            if (indexQuery.getDirection() == OrderDirection.DESC) {
                Collections.reverse(rows);
            }
        }
    }

    int skip = (indexQuery.getSkip());
    if (skip < rows.size()) {
        int limit = indexQuery.getLimit();
        if ((limit > 0) && (rows.size() - skip > limit)) {
            res.setRows(rows.subList(skip, skip + limit));
        } else
            res.setRows(rows);
    } // else all rows are skipped
    return res;
}

From source file:step.core.accessors.Collection.java

License:Open Source License

public CollectionFind<Document> find(Bson query, SearchOrder order, Integer skip, Integer limit) {
    //      StringBuilder query = new StringBuilder();
    //      List<Object> parameters = new ArrayList<>();
    //      if(queryFragments!=null&&queryFragments.size()>0) {
    //         query.append("{$and:[");
    //         Iterator<String> it = queryFragments.iterator();
    //         while(it.hasNext()) {
    //            String criterium = it.next();
    //            query.append("{"+criterium+"}");
    //            if(it.hasNext()) {
    //               query.append(",");
    //            }
    //         }/*  ww w. j  a v a  2 s .  c  o  m*/
    //         query.append("]}");
    //      }

    //      StringBuilder sort = new StringBuilder();
    //      sort.append("{").append(order.getAttributeName()).append(":")
    //         .append(Integer.toString(order.getOrder())).append("}");

    long count = collection.count();

    CountOptions option = new CountOptions();
    option.skip(0).limit(DEFAULT_LIMIT);
    long countResults = collection.count(query, option);

    FindIterable<Document> find = collection.find(query);
    if (order != null) {
        Document sortDoc = new Document(order.getAttributeName(), order.getOrder());
        find.sort(sortDoc);
    }
    if (skip != null) {
        find.skip(skip);
    }
    if (limit != null) {
        find.limit(limit);
    }
    return new CollectionFind<Document>(count, countResults, find.iterator());
}