Java Code Examples for com.mongodb.client.FindIterable#limit()

The following examples show how to use com.mongodb.client.FindIterable#limit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PanacheQueryImpl.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private void manageOffsets(FindIterable find, Integer limit) {
    if (range != null) {
        find.skip(range.getStartIndex());
        if (limit == null) {
            // range is 0 based, so we add 1 to the limit
            find.limit(range.getLastIndex() - range.getStartIndex() + 1);
        }
    } else if (page != null) {
        find.skip(page.index * page.size);
        if (limit == null) {
            find.limit(page.size);
        }
    }
    if (limit != null) {
        find.limit(limit);
    }
}
 
Example 2
Source File: MongoImpl.java    From tephra with MIT License 6 votes vote down vote up
@Override
public JSONArray find(String key, String collection, JSONObject where, int limit, int skip) {
    MongoCollection<Document> mc = getCollection(key, collection);
    if (mc == null)
        return new JSONArray();

    FindIterable<Document> fi = mc.find(toDocument(where));
    if (limit > 0)
        fi.limit(limit);
    if (skip > 0)
        fi.skip(skip);

    JSONArray array = new JSONArray();
    for (Document document : fi)
        array.add(JSON.parseObject(document.toJson()));

    return array;
}
 
Example 3
Source File: MDbOperation.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
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 );
 }
 
Example 4
Source File: ChronoGraph.java    From epcis with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: FindOptions.java    From morphia with Apache License 2.0 5 votes vote down vote up
/**
 * @param iterable the iterable to use
 * @param mapper   the mapper to use
 * @param type     the result type
 * @param <T>      the result type
 * @return the iterable instance for the query results
 * @morphia.internal
 */
public <T> FindIterable<T> apply(final FindIterable<T> iterable, final Mapper mapper, final Class<?> type) {
    if (projection != null) {
        iterable.projection(projection.map(mapper, type));
    }

    iterable.batchSize(batchSize);
    iterable.collation(collation);
    iterable.comment(comment);
    if (cursorType != null) {
        iterable.cursorType(cursorType);
    }
    iterable.hint(hint);
    iterable.hintString(hintString);
    iterable.limit(limit);
    iterable.max(max);
    iterable.maxAwaitTime(maxAwaitTimeMS, TimeUnit.MILLISECONDS);
    iterable.maxTime(maxTimeMS, TimeUnit.MILLISECONDS);
    iterable.min(min);
    iterable.noCursorTimeout(noCursorTimeout);
    iterable.oplogReplay(oplogReplay);
    iterable.partial(partial);
    iterable.returnKey(returnKey);
    iterable.showRecordId(showRecordId);
    iterable.skip(skip);
    if (sort != null) {
        Document mapped = new Document();
        MappedClass mappedClass = mapper.getMappedClass(type);
        for (final Entry<String, Object> entry : sort.entrySet()) {
            Object value = entry.getValue();
            boolean metaScore = value instanceof Document && ((Document) value).get("$meta") != null;
            mapped.put(new PathTarget(mapper, mappedClass, entry.getKey(), !metaScore).translatedPath(), value);
        }
        iterable.sort(mapped);
    }
    return iterable;
}
 
Example 6
Source File: GetMongo.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@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).getValue()) : null;
    final Document projection = context.getProperty(PROJECTION).isSet() ? Document.parse(context.getProperty(PROJECTION).getValue()) : null;
    final Document sort = context.getProperty(SORT).isSet() ? Document.parse(context.getProperty(SORT).getValue()) : null;

    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).asInteger());
        }
        if (context.getProperty(BATCH_SIZE).isSet()) {
            it.batchSize(context.getProperty(BATCH_SIZE).asInteger());
        }

        final MongoCursor<Document> cursor = it.iterator();
        try {
            FlowFile flowFile = null;
            while (cursor.hasNext()) {
                flowFile = session.create();
                flowFile = session.write(flowFile, new OutputStreamCallback() {
                    @Override
                    public void process(OutputStream out) throws IOException {
                        IOUtils.write(cursor.next().toJson(), out);
                    }
                });

                session.getProvenanceReporter().receive(flowFile, context.getProperty(URI).getValue());
                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);
    }
}
 
Example 7
Source File: QueryConverter.java    From sql-to-mongo-db-query-converter with Apache License 2.0 4 votes vote down vote up
/**
 * @param mongoDatabase the database to run the query against.
 * @param <T> variable based on the type of query run.
 * @return When query does a find will return QueryResultIterator&lt;{@link org.bson.Document}&gt;
 *           When query does a count will return a Long
 *           When query does a distinct will return QueryResultIterator&lt;{@link java.lang.String}&gt;
 * @throws ParseException when the sql query cannot be parsed
 */
@SuppressWarnings("unchecked")
public <T> T run(MongoDatabase mongoDatabase) throws ParseException {
    MongoDBQueryHolder mongoDBQueryHolder = getMongoQuery();

    MongoCollection mongoCollection = mongoDatabase.getCollection(mongoDBQueryHolder.getCollection());

    if (SQLCommandType.SELECT.equals(mongoDBQueryHolder.getSqlCommandType())) {
    	
        if (mongoDBQueryHolder.isDistinct()) {
            return (T) new QueryResultIterator<>(mongoCollection.distinct(getDistinctFieldName(mongoDBQueryHolder), mongoDBQueryHolder.getQuery(), String.class));
        } else if (sqlCommandInfoHolder.isCountAll() && !isAggregate(mongoDBQueryHolder)) {
            return (T) Long.valueOf(mongoCollection.count(mongoDBQueryHolder.getQuery()));
        } else if (isAggregate(mongoDBQueryHolder)) {
            
            AggregateIterable aggregate = mongoCollection.aggregate(generateAggSteps(mongoDBQueryHolder,sqlCommandInfoHolder));

            if (aggregationAllowDiskUse != null) {
                aggregate.allowDiskUse(aggregationAllowDiskUse);
            }

            if (aggregationBatchSize != null) {
                aggregate.batchSize(aggregationBatchSize);
            }

            return (T) new QueryResultIterator<>(aggregate);
        } else {
            FindIterable findIterable = mongoCollection.find(mongoDBQueryHolder.getQuery()).projection(mongoDBQueryHolder.getProjection());
            if (mongoDBQueryHolder.getSort() != null && mongoDBQueryHolder.getSort().size() > 0) {
                findIterable.sort(mongoDBQueryHolder.getSort());
            }
            if (mongoDBQueryHolder.getOffset() != -1) {
                findIterable.skip((int) mongoDBQueryHolder.getOffset());
            }
            if (mongoDBQueryHolder.getLimit() != -1) {
                findIterable.limit((int) mongoDBQueryHolder.getLimit());
            }

            return (T) new QueryResultIterator<>(findIterable);
        }
    } else if (SQLCommandType.DELETE.equals(mongoDBQueryHolder.getSqlCommandType())) {
        DeleteResult deleteResult = mongoCollection.deleteMany(mongoDBQueryHolder.getQuery());
        return (T)((Long)deleteResult.getDeletedCount());
    } else {
        throw new UnsupportedOperationException("SQL command type not supported");
    }
}
 
Example 8
Source File: MongoCollectionFind.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	MongoDatabase	db	= getMongoDatabase( _session, argStruct );
	
	String collection	= getNamedStringParam(argStruct, "collection", null);
	if ( collection == null )
		throwException(_session, "please specify a collection");
	
	cfData	query	= getNamedParam(argStruct, "query", null );
	if ( query == null )
		throwException(_session, "please specify query");
	
	int	size			= getNamedIntParam(argStruct, "size", -1 );
	int	skip			= getNamedIntParam(argStruct, "skip", -1 );
	cfData sort		= getNamedParam(argStruct, 		"sort", null );
	cfData fields	= getNamedParam(argStruct, 		"fields", null );
	
	try{
		MongoCollection<Document> col = db.getCollection(collection);

		// Get the initial cursor
		FindIterable<Document>	cursor;
		long start = System.currentTimeMillis();
		Document qry = getDocument(query);
		
		cursor = col.find( qry );
		
		if ( fields != null )
			cursor = cursor.projection( getDocument(fields) );

		// Are we sorting?
		if ( sort != null )
			cursor	= cursor.sort( getDocument(sort) );
		
		// Are we limiting
		if ( skip != -1 )
			cursor	= cursor.skip(skip);
		
		// How many we bringing back
		if ( size != -1 )
			cursor = cursor.limit(size);

		// Now we can run the query
		cfArrayData	results	= cfArrayData.createArray(1);
		
		cursor.forEach( new Block<Document>() {

			@SuppressWarnings( "rawtypes" )
			@Override
			public void apply( final Document st ) {
				try {
					results.addElement( tagUtils.convertToCfData( (Map)st ) );
				} catch ( cfmRunTimeException e ) {}
			}
		} );
		
		_session.getDebugRecorder().execMongo(col, "find", qry, System.currentTimeMillis()-start);
		
		return results;
	} catch (MongoException me){
		throwException(_session, me.getMessage());
		return null;
	}
}
 
Example 9
Source File: MDbMetaData.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns all fields' name and corresponding metadata found in the specified collection.
 * @param collectionName name of MongoDB collection (i.e. table)
 * @param searchLimit maximum number of documents, i.e. rows to search for available fields;
 *          a zero or negative value would adopt the default limit 
 * @param runtimeProps  an instance of QueryProperties containing the data set runtime property values;
 *          may be null to apply all default values in finding the available fields metadata
 * @return  the DocumentsMetaData object that contains the list of available field names and 
 *          corresponding metadata; 
 *          an empty list is returned if no available fields are found, or 
 *          if the specified collection does not exist
 * @throws OdaException
 */
public DocumentsMetaData getAvailableFields( String collectionName, 
        int searchLimit, QueryProperties runtimeProps )
    throws OdaException
{
    MongoCollection<Document> collection = getCollection( collectionName );
    if( collection == null && ! runtimeProps.hasRunCommand() )
    {
        if( runtimeProps.getOperationType() == CommandOperationType.RUN_DB_COMMAND &&
            runtimeProps.getOperationExpression().isEmpty() )
            throw new OdaException( 
                Messages.bind( Messages.mDbMetaData_missingCmdExprText, runtimeProps.getOperationType().displayName() ));
        else
            throw new OdaException( 
                Messages.bind( Messages.mDbMetaData_invalidCollectionName, collectionName ));
    }

    if( searchLimit <= 0 )  // no limit specified, applies meta data design-time default
        searchLimit = DEFAULT_META_DATA_SEARCH_LIMIT;
                    
    // handle optional command operation
    if( runtimeProps.hasValidCommandOperation() )
    {
        QueryModel.validateCommandSyntax( runtimeProps.getOperationType(), 
                            runtimeProps.getOperationExpression() );

        Iterable<Document> commandResults = null;
        if( runtimeProps.hasAggregateCommand() )
            commandResults = MDbOperation.callAggregateCmd( collection, runtimeProps );
        else if( runtimeProps.hasMapReduceCommand() )
        {
            commandResults  = MDbOperation.callMapReduceCmd( collection, runtimeProps );                
            // skip running $query on output collection in discovering metadata
        }
        else if( runtimeProps.hasRunCommand() )
            commandResults = MDbOperation.callDBCommand( m_connectedDB, runtimeProps );
        
        if( commandResults != null )
            return getMetaData( commandResults, searchLimit );
        return sm_emptyFields;
    }

    // run search query operation by default
    FindIterable<Document> rowsCursor = collection.find();

    if( searchLimit > 0 )
        rowsCursor.limit( searchLimit );
    
    QueryProperties mdCursorProps = runtimeProps != null ?
                        runtimeProps :
                        QueryProperties.defaultValues();
    MDbOperation.applyPropertiesToCursor( rowsCursor, mdCursorProps, false );        

    return getMetaData( rowsCursor );
}
 
Example 10
Source File: GetMongoRecord.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile input = null;

    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);
        }
    }
}