當前位置: 首頁>>代碼示例>>Java>>正文


Java FindIterable.limit方法代碼示例

本文整理匯總了Java中com.mongodb.client.FindIterable.limit方法的典型用法代碼示例。如果您正苦於以下問題:Java FindIterable.limit方法的具體用法?Java FindIterable.limit怎麽用?Java FindIterable.limit使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.mongodb.client.FindIterable的用法示例。


在下文中一共展示了FindIterable.limit方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: findOne

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
/**
 * 查詢一個
 *
 * @param collectionName 集合名
 * @param query          查詢條件
 * @param fields         返回字段或者排除字段
 * @param sort
 * @return
 */
public Map<String, Object> findOne(
        String collectionName,
        MongodbQuery query, MongodbFields fields, MongodbSort sort) {
    MongoCollection<Document> collection = sMongoDatabase.getCollection(collectionName);
    FindIterable<Document> findIterable = collection.find(query.getQuery());
    if (fields == null) {
        findIterable.projection(new MongodbFields().getDbObject());
    } else {
        findIterable.projection(fields.getDbObject());
    }
    if (sort != null) {
        findIterable.sort(sort.getDbObject());
    }
    findIterable.limit(1);
    MongoCursor<Document> cursor = findIterable.iterator();
    try {
        if (cursor.hasNext()) {
            return cursor.next();
        }
    } finally {
        cursor.close();
    }
    return null;
}
 
開發者ID:wxz1211,項目名稱:dooo,代碼行數:34,代碼來源:MongodbDataAccess.java

示例2: find

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
/**
 * 查找指定條數的數據
 */
public <T> List<T> find(String collectionName, Integer pageNumber, Integer pageSize, Class<T> clazz) {
    MongoCollection collection = mongoDatabase.getCollection(collectionName);
    List<T> list = new ArrayList<>();
    if (collection == null) {
        return list;
    }
    FindIterable findIterable = collection.find();

    if (pageSize != null && pageSize >= 0) {
        if (pageNumber != null && pageNumber >= 1) {
            findIterable = findIterable.skip((pageNumber - 1) * pageSize);
        }
        findIterable = findIterable.limit(pageSize);
    }
    Iterator<Document> iterator = findIterable.iterator();
    while (iterator.hasNext()) {
        Document document = iterator.next();
        document.remove("_id");
        T t = JSON.parseObject(document.toJson(), clazz);
        list.add(t);
    }
    return list;
}
 
開發者ID:wxz1211,項目名稱:dooo,代碼行數:27,代碼來源:SimpleMongodbAccessor.java

示例3: query

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
private FindIterable<Document> query(final Bson filter, final Bson sort, final Bson projection, final int offset, final int count) {
    if (offset < 0 || count < 0) {
        throw new IllegalArgumentException("offset (" + offset + ") and count(" + count + ") can't be negative");
    }

    FindIterable<Document> findIterable = coll.find(filter);

    if (projection != null) {
        findIterable = findIterable.projection(projection);
    }

    if (sort != null) {
        findIterable = findIterable.sort(sort);
    }

    if (offset > 0) {
        findIterable = findIterable.skip(offset);
    }

    if (count < Integer.MAX_VALUE) {
        findIterable = findIterable.limit(count);
    }

    return findIterable;
}
 
開發者ID:landawn,項目名稱:AbacusUtil,代碼行數:26,代碼來源:MongoCollectionExecutor.java

示例4: applyPropertiesToCursor

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
private void applyPropertiesToCursor( MongoIterable<Document> mongoIterable, QueryProperties queryProps, 
         boolean includeMetaDataSearchLimit, boolean includeSortExpr )
 {
     if( includeMetaDataSearchLimit )
     {
         Integer searchLimit = getModel().getEffectiveMDSearchLimit( queryProps ); 
         if( searchLimit > 0 )
{
             // Apply to FindIterable or MapReduceIterable
	if ( mongoIterable instanceof FindIterable )
	{
		FindIterable<Document> findIterable = (FindIterable<Document>) mongoIterable;
		findIterable.limit( searchLimit.intValue( ) );
	}
	else if ( mongoIterable instanceof MapReduceIterable )
	{
		MapReduceIterable<Document> mapReduceIterable = (MapReduceIterable<Document>) mongoIterable;
		mapReduceIterable.limit( searchLimit.intValue( ) );
	}       
}
     }
     
     applyPropertiesToCursor( mongoIterable, queryProps, includeSortExpr );
 }
 
開發者ID:eclipse,項目名稱:birt,代碼行數:25,代碼來源:MDbOperation.java

示例5: getDocumentMongoCursor

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
private MongoCursor<Document> getDocumentMongoCursor(Table table, List<FilterItem> whereItems, int firstRow,
        int maxRows, Consumer<FilterItem> filterItemsToPostProcessConsumer) {
    final MongoCollection<Document> collection = _mongoDb.getCollection(table.getName());

    final Document query = createMongoDbQuery(table, whereItems, filterItemsToPostProcessConsumer);

    logger.info("Executing MongoDB 'find' query: {}", query);
    FindIterable<Document> iterable = collection.find(query);

    if (maxRows > 0) {
        iterable = iterable.limit(maxRows);
    }
    if (firstRow > 1) {
        final int skip = firstRow - 1;
        iterable = iterable.skip(skip);
    }

    return iterable.iterator();
}
 
開發者ID:apache,項目名稱:metamodel,代碼行數:20,代碼來源:MongoDbDataContext.java

示例6: find

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
/**
 * 查詢
 *
 * @param collectionName 集合名
 * @param query          查詢條件
 * @param fields         返回字段或者排除字段
 * @param sort           排序方式
 * @param pageInfo       分頁
 * @return
 */
public List<Map<String, Object>> find(
        String collectionName,
        MongodbQuery query, MongodbFields fields,
        MongodbSort sort, MongodbPageInfo pageInfo) {
    List<Map<String, Object>> resultMapList = new ArrayList<Map<String, Object>>();
    MongoCollection<Document> collection = sMongoDatabase.getCollection(collectionName);
    FindIterable<Document> findIterable = collection.find(query == null ? new Document() : query.getQuery());
    if (fields == null) {
        findIterable.projection(new MongodbFields().getDbObject());
    } else {
        findIterable.projection(fields.getDbObject());
    }
    if (sort != null) {
        findIterable.sort(sort.getDbObject());
    }
    if (pageInfo != null) {
        int startPos = pageInfo.getPageIndex() * pageInfo.getPageSize();
        int rows = pageInfo.getPageSize();
        if (startPos > 0) {
            findIterable.skip(startPos - 1);
        }
        findIterable.limit(rows);
    }
    MongoCursor<Document> cursor = findIterable.iterator();
    try {
        while (cursor.hasNext()) {
            Map<String, Object> document = cursor.next();
            resultMapList.add(document);
        }
    } finally {
        cursor.close();
    }

    return resultMapList;
}
 
開發者ID:wxz1211,項目名稱:dooo,代碼行數:46,代碼來源:MongodbDataAccess.java

示例7: fetchDataFromBackEnd

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
protected List<T> fetchDataFromBackEnd(Query<T, Bson> query) {
    Bson filter = getFilter(query);
    FindIterable<T> findIterable = mongoCollection.find();
    if (filter != null) findIterable = findIterable.filter(filter);
    findIterable = findIterable.skip(query.getOffset());
    findIterable = findIterable.limit(query.getLimit());
    findIterable = findIterable.sort(getSorts(query.getSortOrders()));

    final List<T> data = new ArrayList<T>();
    findIterable.iterator().forEachRemaining(data::add);
    return data;
}
 
開發者ID:andyphillips404,項目名稱:awplab-core,代碼行數:13,代碼來源:MongoDataProvider.java

示例8: getDocuments

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
public List<Document> getDocuments(List<KeyValue> cond, RESOURCE_TYPE resType, String sortKey, boolean asc, int limit) {

		ArrayList<Document> docList = new ArrayList<Document>();
		
		Iterator<KeyValue> it = cond.iterator();
		KeyValue kv = it.next();
		BasicDBObject query = new BasicDBObject(kv.getKey(), kv.getValue());
		while (it.hasNext()) {
			kv = it.next();
			query.append(kv.getKey(), kv.getValue());
		}
	
		BasicDBObject sort = null;
		if (sortKey != null) {
			sort = new BasicDBObject(sortKey, asc ? 1 : -1);
		}

		MongoCollection<Document> collection = context.getDatabaseManager()
				.getCollection(collectionName);
		MongoCursor<Document> cursor;
		FindIterable<Document> find = collection.find(query);
		if (sort != null) {
			find = find.sort(sort);			
		}
		if (limit >= 0) {
			find = find.limit(limit);
		}
		cursor = find.iterator();
				
		while (cursor.hasNext()) {
			docList.add(cursor.next());
		}

		return docList;

	}
 
開發者ID:iotoasis,項目名稱:SI,代碼行數:37,代碼來源:ResourceDAO.java

示例9: iterator

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
@Override
public Iterator<Document> iterator() {
    FindIterable fi = dc.getCollection().find(query.getQuery()).sort(query.getSort());
    if (limit != -1) {
        fi = fi.limit(limit);
    }
    if (skip != -1) {
        fi = fi.skip(skip);
    }

    cursor = fi.iterator();
    return new Iterator<Document>() {
        @Override
        public boolean hasNext() {
            return cursor.hasNext();
        }

        @Override
        public Document next() {
            org.bson.Document dbObject = (org.bson.Document) cursor.next();
            return dc.getDocumentForBSON(dbObject);
        }

        @Override
        public void remove() {
            cursor.remove();
        }
    };
}
 
開發者ID:KAOREND,項目名稱:reactive-hamster,代碼行數:30,代碼來源:DocumentCursor.java

示例10: read

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
@ExtDirectMethod(STORE_READ)
public ExtDirectStoreResult<User> read(ExtDirectStoreReadRequest request) {

	List<Bson> andFilters = new ArrayList<>();
	StringFilter filter = request.getFirstFilterForField("filter");
	if (filter != null) {
		List<Bson> orFilters = new ArrayList<>();
		orFilters.add(Filters.regex(CUser.loginName, filter.getValue(), "i"));
		orFilters.add(Filters.regex(CUser.lastName, filter.getValue(), "i"));
		orFilters.add(Filters.regex(CUser.firstName, filter.getValue(), "i"));
		orFilters.add(Filters.regex(CUser.email, filter.getValue(), "i"));

		andFilters.add(Filters.or(orFilters));
	}
	andFilters.add(Filters.eq(CUser.deleted, false));

	long total = this.mongoDb.getCollection(User.class)
			.count(Filters.and(andFilters));

	FindIterable<User> find = this.mongoDb.getCollection(User.class)
			.find(Filters.and(andFilters));
	find.sort(Sorts.orderBy(QueryUtil.getSorts(request)));
	find.skip(request.getStart());
	find.limit(request.getLimit());

	return new ExtDirectStoreResult<>(total, QueryUtil.toList(find));
}
 
開發者ID:ralscha,項目名稱:eds-starter6-mongodb,代碼行數:28,代碼來源:UserService.java

示例11: getCachedChronoVertices

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
public ArrayList<CachedChronoVertex> getCachedChronoVertices(BsonArray filters, String sortKey, Boolean isDesc,
		Integer limit) {
	ArrayList<CachedChronoVertex> vList = new ArrayList<CachedChronoVertex>();
	// Merge All the queries with $and
	CachedChronoGraph g = new CachedChronoGraph();
	BsonDocument baseQuery = new BsonDocument();
	FindIterable<BsonDocument> cursor;
	if (filters.isEmpty() == false) {
		baseQuery.put("$and", filters);
		cursor = vertices.find(baseQuery);
	} else {
		cursor = vertices.find();
	}
	if (sortKey != null) {
		if (isDesc == null)
			cursor.sort(new BsonDocument(sortKey, new BsonInt32(-1)));
		else if (isDesc == true) {
			cursor.sort(new BsonDocument(sortKey, new BsonInt32(-1)));
		} else
			cursor.sort(new BsonDocument(sortKey, new BsonInt32(1)));
	}
	if (limit != null)
		cursor.limit(limit);

	MongoCursor<BsonDocument> iter = cursor.iterator();
	while (iter.hasNext()) {
		BsonDocument doc = iter.next();
		String vid = doc.remove("_id").asString().getValue();
		CachedChronoVertex v = g.getChronoVertex(vid);
		v.setProperties(doc);
		vList.add(v);
	}
	return vList;
}
 
開發者ID:JaewookByun,項目名稱:epcis,代碼行數:35,代碼來源:ChronoGraph.java

示例12: fetch

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
public FindIterable<Document> fetch(DbFilter query, int limit) {
    FindIterable<Document> iterable = getConnection().findSync(getCollection(), query == null ? null : query.toBson());
    if(limit != -1) iterable.limit(limit);
    return iterable;
}
 
開發者ID:Superioz,項目名稱:MooProject,代碼行數:6,代碼來源:DatabaseCollection.java

示例13: onTrigger

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    ComponentLog logger = this.getLogger();

    // Evaluate expression language and create BSON Documents
    Document query = (queryProperty.isSet()) ? Document.parse(queryProperty.evaluateAttributeExpressions(flowFile).getValue()) : null;
    Document projection = (projectionProperty.isSet()) ? Document.parse(projectionProperty.evaluateAttributeExpressions(flowFile).getValue()) : null;
    Document sort = (sortProperty.isSet()) ? Document.parse(sortProperty.evaluateAttributeExpressions(flowFile).getValue()) : null;

    try {
        FindIterable<Document> it = (query != null) ? collection.find(query) : collection.find();

        // Apply projection if needed
        if (projection != null) {
            it.projection(projection);
        }

        // Apply sort if needed
        if (sort != null) {
            it.sort(sort);
        }

        // Apply limit if set
        if (limit != null) {
            it.limit(limit.intValue());
        }

        // Iterate and create flowfile for each result
        final MongoCursor<Document> cursor = it.iterator();
        try {
            while (cursor.hasNext()) {
                // Create new flowfile with all parent attributes
                FlowFile ff = session.clone(flowFile);
                ff = session.write(ff, new OutputStreamCallback() {
                    @Override
                    public void process(OutputStream outputStream) throws IOException {
                        IOUtils.write(cursor.next().toJson(), outputStream);
                    }
                });

                session.transfer(ff, REL_SUCCESS);
            }
        } finally {
            cursor.close();
            session.remove(flowFile);
        }

    } catch (Exception e) {
        logger.error("Failed to execute query {} due to {}.", new Object[]{query, e}, e);
        flowFile = session.putAttribute(flowFile, "mongo.exception", e.getMessage());
        session.transfer(flowFile, REL_FAILURE);
    }
}
 
開發者ID:Asymmetrik,項目名稱:nifi-nars,代碼行數:59,代碼來源:QueryMongo.java

示例14: runNativeQuery

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
@Override
public RaptureQueryResult runNativeQuery(final String repoType, final List<String> queryParams) {
    if (repoType.toUpperCase().equals(MONGODB)) {

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

            @Override
            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));
    }
}
 
開發者ID:RapturePlatform,項目名稱:Rapture,代碼行數:49,代碼來源:MongoDbDataStore.java

示例15: createDoFindAll

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
private Function<Exchange, Object> createDoFindAll() {
    return exchange1 -> {
        Iterable<BasicDBObject> result;
        MongoCollection<BasicDBObject> dbCol = calculateCollection(exchange1);
        // do not use getMandatoryBody, because if the body is empty we want to retrieve all objects in the collection
        BasicDBObject query = null;
        // do not run around looking for a type converter unless there is a need for it
        if (exchange1.getIn().getBody() != null) {
            query = exchange1.getIn().getBody(BasicDBObject.class);
        }
        BasicDBObject fieldFilter = exchange1.getIn().getHeader(MongoDbConstants.FIELDS_FILTER, BasicDBObject.class);

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

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

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

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

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

        if (!MongoDbOutputType.DBCursor.equals(endpoint.getOutputType())) {
            try {
                result = new ArrayList<>();
                ret.iterator().forEachRemaining(((List<BasicDBObject>) result)::add);
                exchange1.getOut().setHeader(MongoDbConstants.RESULT_PAGE_SIZE, ((List<BasicDBObject>) result).size());
            } finally {
                ret.iterator().close();
            }
        } else {
            result = ret;
        }
        return result;
    };
}
 
開發者ID:HydAu,項目名稱:Camel,代碼行數:57,代碼來源:MongoDbProducer.java


注:本文中的com.mongodb.client.FindIterable.limit方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。