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

The following examples show how to use com.mongodb.client.FindIterable#projection() . 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: MDbOperation.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
protected MDbResultSet execute() throws OdaException
 {
     if( m_queryObj == null || m_queryCollection == null )
         throw new OdaException( Messages.mDbOp_invalidQueryExpr );

     try
     {
         FindIterable<Document> findIterable = m_queryCollection.find( m_queryObj);
findIterable = findIterable.projection( m_fieldsObj );

         // no search limit applies here; 
         // defer to MDbResultSet to set DBCursor#limit based on its maxRows
         applyPropertiesToCursor( findIterable, getModel().getQueryProperties(), false, true );

         return new MDbResultSet( findIterable.iterator(), getResultSetMetaData(), getModel().getQueryProperties() );
     }
     catch( RuntimeException ex )
     {
         DriverUtil.getLogger().log( Level.SEVERE, "Encountered RuntimeException: ", ex ); //$NON-NLS-1$
         throw new OdaException( ex );
     }        
 }
 
Example 2
Source File: MongoCollectionFindOne.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings( "rawtypes" )
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 to find");
	
	cfData fields	= getNamedParam(argStruct, "fields", null );
	
	try{
		MongoCollection<Document> col = db.getCollection(collection);
		
		long start = System.currentTimeMillis();
		Document qry = getDocument(query);
		
		FindIterable<Document> cursor	= col.find( qry ).limit( 1 );
		
		if ( fields != null )
			cursor = cursor.projection( getDocument(fields) );

		_session.getDebugRecorder().execMongo(col, "findone", qry, System.currentTimeMillis()-start);
		return tagUtils.convertToCfData( (Map)cursor.first() );
		
	} catch (MongoException me){
		throwException(_session, me.getMessage());
		return null;
	}
}
 
Example 3
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 4
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 5
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 6
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);
        }
    }
}