Search in sources :

Example 1 with BatchCursor

use of com.mongodb.internal.operation.BatchCursor in project mongo-java-driver by mongodb.

the class DBCollection method aggregate.

/**
 * Method implements aggregation framework.
 *
 * @param pipeline       operations to be performed in the aggregation pipeline
 * @param options        options to apply to the aggregation
 * @param readPreference {@link ReadPreference} to be used for this operation
 * @return the aggregation operation's result set
 * @mongodb.driver.manual core/aggregation-pipeline/ Aggregation
 * @mongodb.server.release 2.2
 */
public Cursor aggregate(final List<? extends DBObject> pipeline, final AggregationOptions options, final ReadPreference readPreference) {
    Cursor result;
    notNull("options", options);
    List<BsonDocument> stages = preparePipeline(pipeline);
    BsonValue outCollection = stages.get(stages.size() - 1).get("$out");
    if (outCollection != null) {
        AggregateToCollectionOperation operation = new AggregateToCollectionOperation(getNamespace(), stages, getReadConcern(), getWriteConcern()).maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS).allowDiskUse(options.getAllowDiskUse()).bypassDocumentValidation(options.getBypassDocumentValidation()).collation(options.getCollation());
        try {
            executor.execute(operation, getReadPreference(), getReadConcern());
            result = new DBCursor(database.getCollection(outCollection.asString().getValue()), new BasicDBObject(), new DBCollectionFindOptions().readPreference(primary()).collation(options.getCollation()));
        } catch (MongoWriteConcernException e) {
            throw createWriteConcernException(e);
        }
    } else {
        AggregateOperation<DBObject> operation = new AggregateOperation<DBObject>(getNamespace(), stages, getDefaultDBObjectCodec()).maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS).allowDiskUse(options.getAllowDiskUse()).batchSize(options.getBatchSize()).collation(options.getCollation()).retryReads(retryReads);
        BatchCursor<DBObject> cursor1 = executor.execute(operation, readPreference, getReadConcern());
        result = new MongoCursorAdapter(new MongoBatchCursorAdapter<DBObject>(cursor1));
    }
    return result;
}
Also used : AggregateToCollectionOperation(com.mongodb.internal.operation.AggregateToCollectionOperation) DBCollectionFindOptions(com.mongodb.client.model.DBCollectionFindOptions) BatchCursor(com.mongodb.internal.operation.BatchCursor) MapReduceBatchCursor(com.mongodb.internal.operation.MapReduceBatchCursor) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) MongoBatchCursorAdapter(com.mongodb.client.internal.MongoBatchCursorAdapter)

Example 2 with BatchCursor

use of com.mongodb.internal.operation.BatchCursor in project mongo-java-driver by mongodb.

the class DBCollection method distinct.

/**
 * Find the distinct values for a specified field across a collection and returns the results in an array.
 *
 * @param fieldName  Specifies the field for which to return the distinct values
 * @param options    the options to apply for this operation
 * @return A {@code List} of the distinct values
 * @mongodb.driver.manual reference/command/distinct Distinct Command
 * @since 3.4
 */
@SuppressWarnings("unchecked")
public List distinct(final String fieldName, final DBCollectionDistinctOptions options) {
    notNull("fieldName", fieldName);
    return new MongoIterableImpl<BsonValue>(null, executor, options.getReadConcern() != null ? options.getReadConcern() : getReadConcern(), options.getReadPreference() != null ? options.getReadPreference() : getReadPreference(), retryReads) {

        @Override
        public ReadOperation<BatchCursor<BsonValue>> asReadOperation() {
            return new DistinctOperation<BsonValue>(getNamespace(), fieldName, new BsonValueCodec()).filter(wrapAllowNull(options.getFilter())).collation(options.getCollation()).retryReads(retryReads);
        }
    }.map(new Function<BsonValue, Object>() {

        @Override
        public Object apply(final BsonValue bsonValue) {
            if (bsonValue == null) {
                return null;
            }
            BsonDocument document = new BsonDocument("value", bsonValue);
            DBObject obj = getDefaultDBObjectCodec().decode(new BsonDocumentReader(document), DecoderContext.builder().build());
            return obj.get("value");
        }
    }).into(new ArrayList<Object>());
}
Also used : BsonValueCodec(org.bson.codecs.BsonValueCodec) BatchCursor(com.mongodb.internal.operation.BatchCursor) MapReduceBatchCursor(com.mongodb.internal.operation.MapReduceBatchCursor) BsonDocument(org.bson.BsonDocument) BsonDocumentReader(org.bson.BsonDocumentReader) MongoIterableImpl(com.mongodb.client.internal.MongoIterableImpl) BsonValue(org.bson.BsonValue)

Aggregations

BatchCursor (com.mongodb.internal.operation.BatchCursor)2 MapReduceBatchCursor (com.mongodb.internal.operation.MapReduceBatchCursor)2 BsonDocument (org.bson.BsonDocument)2 BsonValue (org.bson.BsonValue)2 MongoBatchCursorAdapter (com.mongodb.client.internal.MongoBatchCursorAdapter)1 MongoIterableImpl (com.mongodb.client.internal.MongoIterableImpl)1 DBCollectionFindOptions (com.mongodb.client.model.DBCollectionFindOptions)1 AggregateToCollectionOperation (com.mongodb.internal.operation.AggregateToCollectionOperation)1 BsonDocumentReader (org.bson.BsonDocumentReader)1 BsonValueCodec (org.bson.codecs.BsonValueCodec)1