use of org.springframework.data.mongodb.core.QueryOperations.AggregationDefinition in project spring-data-mongodb by spring-projects.
the class ReactiveMongoTemplate method doAggregate.
protected <O> Flux<O> doAggregate(Aggregation aggregation, String collectionName, @Nullable Class<?> inputType, Class<O> outputType) {
Assert.notNull(aggregation, "Aggregation pipeline must not be null!");
Assert.hasText(collectionName, "Collection name must not be null or empty!");
Assert.notNull(outputType, "Output type must not be null!");
AggregationOptions options = aggregation.getOptions();
Assert.isTrue(!options.isExplain(), "Cannot use explain option with streaming!");
AggregationDefinition ctx = queryOperations.createAggregation(aggregation, inputType);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Streaming aggregation: %s in collection %s", serializeToJsonSafely(ctx.getAggregationPipeline()), collectionName));
}
ReadDocumentCallback<O> readCallback = new ReadDocumentCallback<>(mongoConverter, outputType, collectionName);
return execute(collectionName, collection -> aggregateAndMap(collection, ctx.getAggregationPipeline(), ctx.isOutOrMerge(), options, readCallback, ctx.getInputType()));
}
use of org.springframework.data.mongodb.core.QueryOperations.AggregationDefinition in project spring-data-mongodb by spring-projects.
the class QueryOperationsUnitTests method createAggregationContextUsesRelaxedOneForTypedAggregationsWhenNoInputTypeProvided.
// GH-3542
@Test
void createAggregationContextUsesRelaxedOneForTypedAggregationsWhenNoInputTypeProvided() {
Aggregation aggregation = Aggregation.newAggregation(Person.class, Aggregation.project("name"));
AggregationDefinition ctx = queryOperations.createAggregation(aggregation, (Class<?>) null);
assertThat(ctx.getAggregationOperationContext()).isInstanceOf(RelaxedTypeBasedAggregationOperationContext.class);
}
use of org.springframework.data.mongodb.core.QueryOperations.AggregationDefinition in project spring-data-mongodb by spring-projects.
the class MongoTemplate method aggregateStream.
@SuppressWarnings("ConstantConditions")
protected <O> CloseableIterator<O> aggregateStream(Aggregation aggregation, String collectionName, Class<O> outputType, @Nullable AggregationOperationContext context) {
Assert.hasText(collectionName, "Collection name must not be null or empty!");
Assert.notNull(aggregation, "Aggregation pipeline must not be null!");
Assert.notNull(outputType, "Output type must not be null!");
Assert.isTrue(!aggregation.getOptions().isExplain(), "Can't use explain option with streaming!");
AggregationDefinition aggregationDefinition = queryOperations.createAggregation(aggregation, context);
AggregationOptions options = aggregation.getOptions();
List<Document> pipeline = aggregationDefinition.getAggregationPipeline();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Streaming aggregation: %s in collection %s", serializeToJsonSafely(pipeline), collectionName));
}
ReadDocumentCallback<O> readCallback = new ReadDocumentCallback<>(mongoConverter, outputType, collectionName);
return execute(collectionName, (CollectionCallback<CloseableIterator<O>>) collection -> {
AggregateIterable<Document> cursor = collection.aggregate(pipeline, Document.class).allowDiskUse(options.isAllowDiskUse());
if (options.getCursorBatchSize() != null) {
cursor = cursor.batchSize(options.getCursorBatchSize());
}
options.getComment().ifPresent(cursor::comment);
options.getHint().ifPresent(cursor::hint);
Class<?> domainType = aggregation instanceof TypedAggregation ? ((TypedAggregation) aggregation).getInputType() : null;
Optionals.firstNonEmpty(options::getCollation, () -> operations.forType(domainType).getCollation()).map(Collation::toMongoCollation).ifPresent(cursor::collation);
return new CloseableIterableCursorAdapter<>(cursor, exceptionTranslator, readCallback);
});
}
use of org.springframework.data.mongodb.core.QueryOperations.AggregationDefinition in project spring-data-mongodb by spring-projects.
the class QueryOperationsUnitTests method createAggregationContextUsesRelaxedOneForUntypedAggregationsWhenInputTypeProvided.
// GH-3542
@Test
void createAggregationContextUsesRelaxedOneForUntypedAggregationsWhenInputTypeProvided() {
Aggregation aggregation = Aggregation.newAggregation(Aggregation.project("name"));
AggregationDefinition ctx = queryOperations.createAggregation(aggregation, Person.class);
assertThat(ctx.getAggregationOperationContext()).isInstanceOf(RelaxedTypeBasedAggregationOperationContext.class);
}
use of org.springframework.data.mongodb.core.QueryOperations.AggregationDefinition in project spring-data-mongodb by spring-projects.
the class QueryOperationsUnitTests method createAggregationContextUsesDefaultIfNoMappingDesired.
// GH-3542
@Test
void createAggregationContextUsesDefaultIfNoMappingDesired() {
Aggregation aggregation = Aggregation.newAggregation(Aggregation.project("name")).withOptions(NO_MAPPING);
AggregationDefinition ctx = queryOperations.createAggregation(aggregation, Person.class);
assertThat(ctx.getAggregationOperationContext()).isEqualTo(Aggregation.DEFAULT_CONTEXT);
}
Aggregations