Search in sources :

Example 1 with AggregateToCollectionOperation

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

the class DBCollection method aggregate.

@SuppressWarnings("deprecation")
private Cursor aggregate(final List<? extends DBObject> pipeline, final AggregationOptions options, final ReadPreference readPreference, final boolean returnCursorForOutCollection) {
    if (options == null) {
        throw new IllegalArgumentException("options can not be null");
    }
    List<BsonDocument> stages = preparePipeline(pipeline);
    BsonValue outCollection = stages.get(stages.size() - 1).get("$out");
    if (outCollection != null) {
        AggregateToCollectionOperation operation = new AggregateToCollectionOperation(getNamespace(), stages, getWriteConcern()).maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS).allowDiskUse(options.getAllowDiskUse()).bypassDocumentValidation(options.getBypassDocumentValidation()).collation(options.getCollation());
        try {
            executor.execute(operation);
            if (returnCursorForOutCollection) {
                return new DBCursor(database.getCollection(outCollection.asString().getValue()), new BasicDBObject(), new DBCollectionFindOptions().readPreference(primary()).collation(options.getCollation()));
            } else {
                return null;
            }
        } catch (MongoWriteConcernException e) {
            throw createWriteConcernException(e);
        }
    } else {
        AggregateOperation<DBObject> operation = new AggregateOperation<DBObject>(getNamespace(), stages, getDefaultDBObjectCodec()).readConcern(getReadConcern()).maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS).allowDiskUse(options.getAllowDiskUse()).batchSize(options.getBatchSize()).useCursor(options.getOutputMode() == com.mongodb.AggregationOptions.OutputMode.CURSOR).collation(options.getCollation());
        BatchCursor<DBObject> cursor = executor.execute(operation, readPreference);
        return new MongoCursorAdapter(new MongoBatchCursorAdapter<DBObject>(cursor));
    }
}
Also used : AggregateToCollectionOperation(com.mongodb.operation.AggregateToCollectionOperation) DBCollectionFindOptions(com.mongodb.client.model.DBCollectionFindOptions) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) AggregateOperation(com.mongodb.operation.AggregateOperation)

Example 2 with AggregateToCollectionOperation

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

the class AggregateIterableImpl method execute.

@SuppressWarnings("deprecation")
private MongoIterable<TResult> execute() {
    List<BsonDocument> aggregateList = createBsonDocumentList();
    BsonValue outCollection = getAggregateOutCollection(aggregateList);
    if (outCollection != null) {
        AggregateToCollectionOperation operation = new AggregateToCollectionOperation(namespace, aggregateList, writeConcern).maxTime(maxTimeMS, MILLISECONDS).allowDiskUse(allowDiskUse).bypassDocumentValidation(bypassDocumentValidation).collation(collation);
        MongoIterable<TResult> delegated = new FindIterableImpl<TDocument, TResult>(new MongoNamespace(namespace.getDatabaseName(), outCollection.asString().getValue()), documentClass, resultClass, codecRegistry, primary(), readConcern, executor, new BsonDocument(), new FindOptions().collation(collation));
        if (batchSize != null) {
            delegated.batchSize(batchSize);
        }
        return new AwaitingWriteOperationIterable<TResult, Void>(operation, executor, delegated);
    } else {
        return new OperationIterable<TResult>(new AggregateOperation<TResult>(namespace, aggregateList, codecRegistry.get(resultClass)).maxTime(maxTimeMS, MILLISECONDS).allowDiskUse(allowDiskUse).batchSize(batchSize).useCursor(useCursor).readConcern(readConcern).collation(collation), readPreference, executor);
    }
}
Also used : AggregateToCollectionOperation(com.mongodb.operation.AggregateToCollectionOperation) FindOptions(com.mongodb.client.model.FindOptions) BsonDocument(org.bson.BsonDocument) MongoNamespace(com.mongodb.MongoNamespace) BsonValue(org.bson.BsonValue)

Example 3 with AggregateToCollectionOperation

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

the class AggregateIterableImpl method toCollection.

@Override
public void toCollection(final SingleResultCallback<Void> callback) {
    List<BsonDocument> aggregateList = createBsonDocumentList();
    BsonValue outCollection = getAggregateOutCollection(aggregateList);
    if (outCollection == null) {
        throw new IllegalStateException("The last stage of the aggregation pipeline must be $out");
    }
    executor.execute(new AggregateToCollectionOperation(namespace, aggregateList, writeConcern).maxTime(maxTimeMS, MILLISECONDS).allowDiskUse(allowDiskUse).collation(collation), callback);
}
Also used : AggregateToCollectionOperation(com.mongodb.operation.AggregateToCollectionOperation) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue)

Aggregations

AggregateToCollectionOperation (com.mongodb.operation.AggregateToCollectionOperation)3 BsonDocument (org.bson.BsonDocument)3 BsonValue (org.bson.BsonValue)3 MongoNamespace (com.mongodb.MongoNamespace)1 DBCollectionFindOptions (com.mongodb.client.model.DBCollectionFindOptions)1 FindOptions (com.mongodb.client.model.FindOptions)1 AggregateOperation (com.mongodb.operation.AggregateOperation)1