List of usage examples for com.mongodb.client FindIterable limit
FindIterable<TResult> limit(int limit);
From source file:io.lumeer.storage.mongodb.MongoDbStorage.java
License:Open Source License
@Override public List<DataDocument> search(String collectionName, DataFilter filter, final DataSort sort, List<String> attributes, final int skip, int limit) { MongoCollection<Document> collection = database.getCollection(collectionName); FindIterable<Document> documents = filter != null ? collection.find(filter.<Bson>get()) : collection.find(); if (sort != null) { documents = documents.sort(sort.<Bson>get()); }/* ww w . j a v a2 s . com*/ if (attributes != null && !attributes.isEmpty()) { documents.projection(Projections.fields(Projections.include(attributes))); } if (skip > 0) { documents = documents.skip(skip); } if (limit > 0) { documents = documents.limit(limit); } return MongoUtils.convertIterableToList(documents); }
From source file:net.springfieldusa.storage.mongodb.comp.MongoStorageComponent.java
License:Open Source License
private <T extends EntityObject> Collection<T> find(String collection, Document query, int skip, int limit, Supplier<T> factory) { FindIterable<Document> cursor = getCollection(collection).find(query); if (skip > 0) cursor.skip(skip);//from w w w.j a va 2 s.c o m if (limit > 0) cursor.limit(limit); Collection<T> items = new ArrayList<>(); for (Document value : cursor) items.add(createObject(factory, value)); return items; }
From source file:net.vpc.app.bdvbbroker.client.BdvbRepositoryClientMongodb.java
License:Open Source License
public List<BdvbPacket> findByDeviceUUID(String ownerUUID, String deviceUUID, Date from, Date to, int maxCount, BdvbPacketTransform transform) { Document allFilter = new Document(); allFilter.put("deviceUUID", deviceUUID); createBsonServerTimeFilter(from, to, allFilter); List<BdvbPacket> list = new ArrayList<>(); Document sortCriteria = new Document(); sortCriteria.put("deviceTime", -1); FindIterable<Document> documents = packets.find(allFilter).sort(sortCriteria); if (maxCount != 0) { documents = documents.limit(Math.abs(maxCount)); }/*from w w w. j a v a 2s.c o m*/ if (maxCount != 0) { int size = 0; if (transform != null) { int maxCount0 = maxCount < 0 ? -maxCount : maxCount; if (maxCount > 0) { for (Document document : documents) { BdvbPacket defaultBdvbPacket = ClientUtils.GSON.fromJson(ClientUtils.GSON.toJson(document), DefaultBdvbPacket.class); defaultBdvbPacket = transform.transform(defaultBdvbPacket); if (defaultBdvbPacket != null) { list.add(defaultBdvbPacket); size++; if (size >= maxCount) { break; } } } } else { LinkedList<BdvbPacket> llist = new LinkedList<>(); for (Document document : documents) { BdvbPacket defaultBdvbPacket = ClientUtils.GSON.fromJson(ClientUtils.GSON.toJson(document), DefaultBdvbPacket.class); defaultBdvbPacket = transform.transform(defaultBdvbPacket); if (defaultBdvbPacket != null) { llist.add(defaultBdvbPacket); size++; if (size > maxCount0) { llist.removeFirst(); } } } list = llist; } } else { for (Document document : documents) { DefaultBdvbPacket defaultBdvbPacket = ClientUtils.GSON .fromJson(ClientUtils.GSON.toJson(document), DefaultBdvbPacket.class); list.add(defaultBdvbPacket); size++; if (size >= maxCount) { break; } } } } else { if (transform != null) { for (Document document : documents) { BdvbPacket defaultBdvbPacket = ClientUtils.GSON.fromJson(ClientUtils.GSON.toJson(document), DefaultBdvbPacket.class); defaultBdvbPacket = transform.transform(defaultBdvbPacket); if (defaultBdvbPacket != null) { list.add(defaultBdvbPacket); } } } else { for (Document document : documents) { DefaultBdvbPacket defaultBdvbPacket = ClientUtils.GSON .fromJson(ClientUtils.GSON.toJson(document), DefaultBdvbPacket.class); list.add(defaultBdvbPacket); } } } Collections.reverse(list); return list; }
From source file:net.vpc.app.bdvbbroker.repository.BdvbRepositoryMongodb.java
License:Open Source License
public List<BdvbPacket> findPackets(String ownerUUID, String deviceUUID, Date from, Date to, int maxCount, BdvbPacketTransform transform) { Document allFilter = new Document(); if (Utils.trimToNull(deviceUUID) != null) { allFilter.put("deviceUUID", deviceUUID); }/*from w ww. j a va2 s .c o m*/ createBsonServerTimeFilter(from, to, allFilter); List<BdvbPacket> list = new ArrayList<>(); Document sortCriteria = new Document(); sortCriteria.put("deviceTime", -1); FindIterable<Document> documents = packets.find(allFilter).sort(sortCriteria); if (maxCount != 0) { documents = documents.limit(Math.abs(maxCount)); } if (maxCount != 0) { int size = 0; if (transform != null) { int maxCount0 = maxCount < 0 ? -maxCount : maxCount; if (maxCount > 0) { for (Document document : documents) { BdvbPacket defaultBdvbPacket = Utils.GSON.fromJson(Utils.GSON.toJson(document), DefaultBdvbPacket.class); defaultBdvbPacket = transform.transform(defaultBdvbPacket); if (defaultBdvbPacket != null) { list.add(defaultBdvbPacket); size++; if (size >= maxCount) { break; } } } } else { LinkedList<BdvbPacket> llist = new LinkedList<>(); for (Document document : documents) { BdvbPacket defaultBdvbPacket = Utils.GSON.fromJson(Utils.GSON.toJson(document), DefaultBdvbPacket.class); defaultBdvbPacket = transform.transform(defaultBdvbPacket); if (defaultBdvbPacket != null) { llist.add(defaultBdvbPacket); size++; if (size > maxCount0) { llist.removeFirst(); } } } list = llist; } } else { for (Document document : documents) { DefaultBdvbPacket defaultBdvbPacket = Utils.GSON.fromJson(Utils.GSON.toJson(document), DefaultBdvbPacket.class); list.add(defaultBdvbPacket); size++; if (size >= maxCount) { break; } } } } else { if (transform != null) { for (Document document : documents) { BdvbPacket defaultBdvbPacket = Utils.GSON.fromJson(Utils.GSON.toJson(document), DefaultBdvbPacket.class); defaultBdvbPacket = transform.transform(defaultBdvbPacket); if (defaultBdvbPacket != null) { list.add(defaultBdvbPacket); } } } else { for (Document document : documents) { DefaultBdvbPacket defaultBdvbPacket = Utils.GSON.fromJson(Utils.GSON.toJson(document), DefaultBdvbPacket.class); list.add(defaultBdvbPacket); } } } Collections.reverse(list); return list; }
From source file:org.apache.metamodel.mongodb.mongo3.MongoDbDataContext.java
License:Apache License
private MongoCursor<Document> getDocumentMongoCursor(Table table, List<FilterItem> whereItems, int firstRow, int maxRows) { final MongoCollection<Document> collection = _mongoDb.getCollection(table.getName()); final Document query = createMongoDbQuery(table, whereItems); logger.info("Executing MongoDB 'find' query: {}", query); FindIterable<Document> iterable = collection.find(query); if (maxRows > 0) { iterable = iterable.limit(maxRows); }/*from w ww . j a v a2 s . c o m*/ if (firstRow > 1) { final int skip = firstRow - 1; iterable = iterable.skip(skip); } return iterable.iterator(); }
From source file:org.apache.nifi.processors.mongodb.GetMongo.java
License:Apache License
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { final ComponentLog logger = getLogger(); final Document query = context.getProperty(QUERY).isSet() ? Document.parse(context.getProperty(QUERY).evaluateAttributeExpressions().getValue()) : null;/*from ww w. j a va 2 s . c o m*/ final Document projection = context.getProperty(PROJECTION).isSet() ? Document.parse(context.getProperty(PROJECTION).evaluateAttributeExpressions().getValue()) : null; final Document sort = context.getProperty(SORT).isSet() ? Document.parse(context.getProperty(SORT).evaluateAttributeExpressions().getValue()) : null; final String jsonTypeSetting = context.getProperty(JSON_TYPE).getValue(); configureMapper(jsonTypeSetting); final MongoCollection<Document> collection = getCollection(context); try { final FindIterable<Document> it = query != null ? collection.find(query) : collection.find(); if (projection != null) { it.projection(projection); } if (sort != null) { it.sort(sort); } if (context.getProperty(LIMIT).isSet()) { it.limit(context.getProperty(LIMIT).evaluateAttributeExpressions().asInteger()); } if (context.getProperty(BATCH_SIZE).isSet()) { it.batchSize(context.getProperty(BATCH_SIZE).evaluateAttributeExpressions().asInteger()); } final MongoCursor<Document> cursor = it.iterator(); ComponentLog log = getLogger(); try { FlowFile flowFile = null; if (context.getProperty(RESULTS_PER_FLOWFILE).isSet()) { int ceiling = context.getProperty(RESULTS_PER_FLOWFILE).evaluateAttributeExpressions() .asInteger(); List<Document> batch = new ArrayList<>(); while (cursor.hasNext()) { batch.add(cursor.next()); if (batch.size() == ceiling) { try { if (log.isDebugEnabled()) { log.debug("Writing batch..."); } String payload = buildBatch(batch, jsonTypeSetting); writeBatch(payload, context, session); batch = new ArrayList<>(); } catch (IOException ex) { getLogger().error("Error building batch", ex); } } } if (batch.size() > 0) { try { writeBatch(buildBatch(batch, jsonTypeSetting), context, session); } catch (IOException ex) { getLogger().error("Error sending remainder of batch", ex); } } } else { while (cursor.hasNext()) { flowFile = session.create(); flowFile = session.write(flowFile, new OutputStreamCallback() { @Override public void process(OutputStream out) throws IOException { String json; if (jsonTypeSetting.equals(JSON_TYPE_STANDARD)) { json = mapper.writerWithDefaultPrettyPrinter() .writeValueAsString(cursor.next()); } else { json = cursor.next().toJson(); } IOUtils.write(json, out); } }); flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), "application/json"); session.getProvenanceReporter().receive(flowFile, getURI(context)); session.transfer(flowFile, REL_SUCCESS); } } session.commit(); } finally { cursor.close(); } } catch (final RuntimeException e) { context.yield(); session.rollback(); logger.error("Failed to execute query {} due to {}", new Object[] { query, e }, e); } }
From source file:org.apache.nifi.processors.mongodb.GetMongoRecord.java
License:Apache License
@Override public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { FlowFile input = null;/*from w w w . jav a2 s .c om*/ if (context.hasIncomingConnection()) { input = session.get(); if (input == null && context.hasNonLoopConnection()) { return; } } final String database = context.getProperty(DATABASE_NAME).evaluateAttributeExpressions(input).getValue(); final String collection = context.getProperty(COLLECTION_NAME).evaluateAttributeExpressions(input) .getValue(); final String schemaName = context.getProperty(SCHEMA_NAME).evaluateAttributeExpressions(input).getValue(); final Document query = getQuery(context, session, input); MongoCollection mongoCollection = clientService.getDatabase(database).getCollection(collection); FindIterable<Document> find = mongoCollection.find(query); if (context.getProperty(SORT).isSet()) { find = find .sort(Document.parse(context.getProperty(SORT).evaluateAttributeExpressions(input).getValue())); } if (context.getProperty(PROJECTION).isSet()) { find = find.projection( Document.parse(context.getProperty(PROJECTION).evaluateAttributeExpressions(input).getValue())); } if (context.getProperty(LIMIT).isSet()) { find = find.limit(context.getProperty(LIMIT).evaluateAttributeExpressions(input).asInteger()); } MongoCursor<Document> cursor = find.iterator(); FlowFile output = input != null ? session.create(input) : session.create(); final FlowFile inputPtr = input; try { final Map<String, String> attributes = getAttributes(context, input, query, mongoCollection); try (OutputStream out = session.write(output)) { Map<String, String> attrs = inputPtr != null ? inputPtr.getAttributes() : new HashMap<String, String>() { { put("schema.name", schemaName); } }; RecordSchema schema = writerFactory.getSchema(attrs, null); RecordSetWriter writer = writerFactory.createWriter(getLogger(), schema, out, attrs); long count = 0L; writer.beginRecordSet(); while (cursor.hasNext()) { Document next = cursor.next(); if (next.get("_id") instanceof ObjectId) { next.put("_id", next.get("_id").toString()); } Record record = new MapRecord(schema, next); writer.write(record); count++; } writer.finishRecordSet(); writer.close(); out.close(); attributes.put("record.count", String.valueOf(count)); } catch (SchemaNotFoundException e) { throw new RuntimeException(e); } output = session.putAllAttributes(output, attributes); session.getProvenanceReporter().fetch(output, getURI(context)); session.transfer(output, REL_SUCCESS); if (input != null) { session.transfer(input, REL_ORIGINAL); } } catch (Exception ex) { ex.printStackTrace(); getLogger().error("Error writing record set from Mongo query.", ex); session.remove(output); if (input != null) { session.transfer(input, REL_FAILURE); } } }
From source file:org.axonframework.mongo.eventsourcing.eventstore.AbstractMongoEventStorageStrategy.java
License:Apache License
@Override public List<? extends TrackedEventData<?>> findTrackedEvents(MongoCollection<Document> eventCollection, TrackingToken lastToken, int batchSize) { FindIterable<Document> cursor; if (lastToken == null) { cursor = eventCollection.find(); } else {//from w w w . j a v a 2s .c om Assert.isTrue(lastToken instanceof MongoTrackingToken, () -> String.format("Token %s is of the wrong type", lastToken)); MongoTrackingToken trackingToken = (MongoTrackingToken) lastToken; cursor = eventCollection.find(and( gte(eventConfiguration.timestampProperty(), trackingToken.getTimestamp().minus(lookBackTime).toString()), nin(eventConfiguration.eventIdentifierProperty(), trackingToken.getKnownEventIds()))); } cursor = cursor.sort(new BasicDBObject(eventConfiguration().timestampProperty(), ORDER_ASC) .append(eventConfiguration().sequenceNumberProperty(), ORDER_ASC)); cursor = cursor.limit(batchSize); AtomicReference<MongoTrackingToken> previousToken = new AtomicReference<>((MongoTrackingToken) lastToken); List<TrackedEventData<?>> results = new ArrayList<>(); for (Document document : cursor) { extractEvents(document) .filter(ed -> previousToken.get() == null || !previousToken.get().getKnownEventIds().contains(ed.getEventIdentifier())) .map(event -> new TrackedMongoEventEntry<>(event, previousToken.updateAndGet(token -> token == null ? MongoTrackingToken.of(event.getTimestamp(), event.getEventIdentifier()) : token.advanceTo(event.getTimestamp(), event.getEventIdentifier(), lookBackTime)))) .forEach(results::add); } return results; }
From source file:org.bananaforscale.cormac.dao.document.DocumentDataServiceImpl.java
License:Apache License
/** * Returns all the documents in a collection. * * @param databaseName the database/*w ww . j av a 2s .co m*/ * @param collectionName the collection * @param query a JSON query param in the style of mongo * @param fields fields to return * @param skip the amount of documents to skip * @param limit the amount of documents to limit the result to * @param orderBy order ascending or descending by property * @param includeId determines whether to include the Mongo "_id" field * @return the documents in a collection * @throws DatasourceException * @throws NotFoundException */ @Override public List<String> getAll(String databaseName, String collectionName, String query, String fields, String skip, String limit, String orderBy, boolean includeId) throws DatasourceException, NotFoundException { try { if (!databaseExists(databaseName)) { throw new NotFoundException("The database doesn't exist in the datasource"); } if (!collectionExists(databaseName, collectionName)) { throw new NotFoundException("The collection doesn't exist in the datasource"); } Integer intSkip, intLimit; try { intSkip = Integer.parseInt(skip); } catch (NumberFormatException ex) { intSkip = 0; } try { intLimit = Integer.parseInt(limit); } catch (NumberFormatException ex) { intLimit = 0; } // 1 or -1 to specify an ascending or descending sort respectively. Document orderByObject = null; if (orderBy != null && !orderBy.isEmpty()) { if (orderBy.contains("ascending")) { String[] parts = orderBy.split(":"); orderByObject = new Document(parts[0], 1); } else if (orderBy.contains("descending")) { String[] parts = orderBy.split(":"); orderByObject = new Document(parts[0], -1); } } MongoDatabase mongoDatabase = mongoClient.getDatabase(databaseName); MongoCollection collection = mongoDatabase.getCollection(collectionName); FindIterable iterable = (query == null || query.isEmpty()) ? collection.find() : collection.find(Document.parse(query)); // TODO: Figure out how to do this in new API // if (fields != null && !fields.isEmpty()) { // // expect the form to be field:value,field:value // Document document = new Document(); // String[] parts = fields.split(","); // for (String part : parts) { // String[] tempParts = part.split(":"); // document.append(tempParts[0], tempParts[1]); // } // iterable.projection(document); // } iterable.skip(intSkip); iterable.limit(intLimit); if (orderByObject != null) { iterable.sort(orderByObject); } Iterator<Document> curIter = iterable.iterator(); List<String> documentList = new ArrayList<>(); while (curIter.hasNext()) { Document current = curIter.next(); if (!includeId) { current.remove("_id"); } documentList.add(JSON.serialize(current)); } return documentList; } catch (MongoException ex) { logger.error("An error occured while retrieving the document list", ex); throw new DatasourceException("An error occured while retrieving the document list"); } }
From source file:org.codinjutsu.tools.nosql.mongo.logic.SingleMongoClient.java
License:Apache License
private MongoResult find(MongoQueryOptions mongoQueryOptions, final MongoResult mongoResult, MongoCollection<Document> collection) { Bson filter = mongoQueryOptions.getFilter(); Bson projection = mongoQueryOptions.getProjection(); Bson sort = mongoQueryOptions.getSort(); FindIterable<Document> findIterable; if (projection == null) { findIterable = collection.find(filter); } else {//from w ww . j a va 2s. co m findIterable = collection.find(filter).projection(projection); } if (sort != null) { findIterable = findIterable.sort(sort); } findIterable.limit(mongoQueryOptions.getResultLimit()); findIterable.forEach(new Block<Document>() { @Override public void apply(Document document) { mongoResult.add(document); } }); return mongoResult; }