use of com.mongodb.operation.AggregateOperation 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));
}
}
Aggregations