本文整理汇总了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;
}
示例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();
}
}
示例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;
}
示例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 );
}
}
示例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;
}
示例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;
}
}
示例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);
}
}
示例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;
}
}
示例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 );
}
}