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


Java FindIterable.projection方法代碼示例

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


在下文中一共展示了FindIterable.projection方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: findAndConsumer

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
/**
 * 查詢並逐條處理
 *
 * @param collectionName 集合名
 * @param query          查詢條件
 * @param fields         返回字段或者排除字段
 * @param sort           排序方式
 * @param consumer       記錄處理
 * @return
 */
public void findAndConsumer(
        String collectionName,
        MongodbQuery query, MongodbFields fields,
        MongodbSort sort, Consumer<Map<String, Object>> consumer) {
    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());
    }
    MongoCursor<Document> cursor = findIterable.iterator();
    try {
        while (cursor.hasNext()) {
            Map<String, Object> document = cursor.next();
            consumer.accept(document);
        }
    } finally {
        cursor.close();
    }
}
 
開發者ID:wxz1211,項目名稱:dooo,代碼行數:35,代碼來源:MongodbDataAccess.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: execute

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
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 );
     }        
 }
 
開發者ID:eclipse,項目名稱:birt,代碼行數:23,代碼來源:MDbOperation.java

示例5: 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

示例6: execute

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
@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;
	}
}
 
開發者ID:OpenBD,項目名稱:openbd-core,代碼行數:34,代碼來源:MongoCollectionFindOne.java

示例7: 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

示例8: execute

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
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;
	}
}
 
開發者ID:OpenBD,項目名稱:openbd-core,代碼行數:64,代碼來源:MongoCollectionFind.java

示例9: prepare

import com.mongodb.client.FindIterable; //導入方法依賴的package包/類
protected void prepare( MongoCollection<Document> dbCollection) 
      throws OdaException
  {
      resetPreparedState();
      
      // get individual query components from data set properties
      QueryProperties queryProps = getModel().getQueryProperties();
      
// Apply read preferences
ReadPreference readPref = queryProps.getTaggableReadPreference( );
if ( readPref != null )
	dbCollection = dbCollection.withReadPreference( readPref );

      // handle findQueryExpr property
      BasicDBObject queryObj = queryProps.getFindQueryExprAsParsedObject();

      if( queryObj == null )
          queryObj = new BasicDBObject();

      // specify fields to retrieve
      BasicDBObject fieldsObj = queryProps.getSelectedFieldsAsProjectionKeys();

      try
      {
          // find search-limited rows to validate the queryObj and 
          // obtain result set metadata on the specified dbCollection
          FindIterable<Document> findIterable = dbCollection.find( queryObj );
	findIterable = findIterable.projection( fieldsObj );
          // set data set query properties on DBCursor
          // to get result set metadata; no need to include sortExpr for getting metadata
          applyPropertiesToCursor( findIterable , queryProps, true, false );

          m_rsMetaData = new MDbResultSetMetaData( findIterable, queryProps.getSelectedFieldNames(),
                                  queryProps.isAutoFlattening() );

          // no exception; the find arguments on specified dbCollection are valid
          m_queryCollection = dbCollection;  // could be different, e.g. mapReduce output collection
          m_fieldsObj = fieldsObj;
          m_queryObj = queryObj;
      }
      catch( RuntimeException ex )
      {
          DriverUtil.getLogger().log( Level.SEVERE, "Encountered RuntimeException in QueryModel#prepareQuery(DBCollection).", ex ); //$NON-NLS-1$
          throw new OdaException( ex );
      }
  }
 
開發者ID:eclipse,項目名稱:birt,代碼行數:47,代碼來源:MDbOperation.java


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