Search in sources :

Example 6 with AggregationOptions

use of org.springframework.data.mongodb.core.aggregation.AggregationOptions 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);
    });
}
Also used : Document(org.bson.Document) EntityCallbacks(org.springframework.data.mapping.callback.EntityCallbacks) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) NumberUtils(org.springframework.util.NumberUtils) BsonUtils(org.springframework.data.mongodb.util.BsonUtils) MongoDatabase(com.mongodb.client.MongoDatabase) BigDecimal(java.math.BigDecimal) QueryContext(org.springframework.data.mongodb.core.QueryOperations.QueryContext) BulkOperationContext(org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext) Optionals(org.springframework.data.util.Optionals) UpdateResult(com.mongodb.client.result.UpdateResult) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) MapReduceResults(org.springframework.data.mongodb.core.mapreduce.MapReduceResults) CursorOption(org.springframework.data.mongodb.core.query.Meta.CursorOption) MongoJsonSchemaMapper(org.springframework.data.mongodb.core.convert.MongoJsonSchemaMapper) Resource(org.springframework.core.io.Resource) AggregationOperationContext(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) ClassUtils(org.springframework.util.ClassUtils) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) AggregateIterable(com.mongodb.client.AggregateIterable) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) CollectionUtils(org.springframework.util.CollectionUtils) LogFactory(org.apache.commons.logging.LogFactory) DistinctQueryContext(org.springframework.data.mongodb.core.QueryOperations.DistinctQueryContext) ApplicationContextAware(org.springframework.context.ApplicationContextAware) MongoDatabaseUtils(org.springframework.data.mongodb.MongoDatabaseUtils) java.util(java.util) Validator(org.springframework.data.mongodb.core.validation.Validator) IndexOperationsProvider(org.springframework.data.mongodb.core.index.IndexOperationsProvider) MongoCollection(com.mongodb.client.MongoCollection) MongoCustomConversions(org.springframework.data.mongodb.core.convert.MongoCustomConversions) GroupByResults(org.springframework.data.mongodb.core.mapreduce.GroupByResults) MappingContext(org.springframework.data.mapping.context.MappingContext) UpdateDefinition(org.springframework.data.mongodb.core.query.UpdateDefinition) Bson(org.bson.conversions.Bson) DbRefResolver(org.springframework.data.mongodb.core.convert.DbRefResolver) MappingException(org.springframework.data.mapping.MappingException) Nullable(org.springframework.lang.Nullable) JsonSchemaMapper(org.springframework.data.mongodb.core.convert.JsonSchemaMapper) com.mongodb.client.model(com.mongodb.client.model) CountContext(org.springframework.data.mongodb.core.QueryOperations.CountContext) org.springframework.data.mongodb.core.mapping.event(org.springframework.data.mongodb.core.mapping.event) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) ObjectUtils(org.springframework.util.ObjectUtils) DeleteContext(org.springframework.data.mongodb.core.QueryOperations.DeleteContext) TypedAggregation(org.springframework.data.mongodb.core.aggregation.TypedAggregation) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) MongoMappingEventPublisher(org.springframework.data.mongodb.core.index.MongoMappingEventPublisher) Criteria(org.springframework.data.mongodb.core.query.Criteria) ResourceUtils(org.springframework.util.ResourceUtils) DistinctIterable(com.mongodb.client.DistinctIterable) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) MongoIterable(com.mongodb.client.MongoIterable) CloseableIterator(org.springframework.data.util.CloseableIterator) Collation(org.springframework.data.mongodb.core.query.Collation) EntityProjection(org.springframework.data.projection.EntityProjection) PersistenceExceptionTranslator(org.springframework.dao.support.PersistenceExceptionTranslator) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) MapReduceOptions(org.springframework.data.mongodb.core.mapreduce.MapReduceOptions) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) MapReduceIterable(com.mongodb.client.MapReduceIterable) AdaptibleEntity(org.springframework.data.mongodb.core.EntityOperations.AdaptibleEntity) MongoWriter(org.springframework.data.mongodb.core.convert.MongoWriter) RoundingMode(java.math.RoundingMode) Meta(org.springframework.data.mongodb.core.query.Meta) ResourceLoader(org.springframework.core.io.ResourceLoader) MongoPersistentEntityIndexCreator(org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator) SerializationUtils(org.springframework.data.mongodb.core.query.SerializationUtils) MongoException(com.mongodb.MongoException) ApplicationListener(org.springframework.context.ApplicationListener) AggregationResults(org.springframework.data.mongodb.core.aggregation.AggregationResults) Collectors(java.util.stream.Collectors) FindIterable(com.mongodb.client.FindIterable) UpdateMapper(org.springframework.data.mongodb.core.convert.UpdateMapper) TypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext) UpdateContext(org.springframework.data.mongodb.core.QueryOperations.UpdateContext) Metric(org.springframework.data.geo.Metric) ReadPreference(com.mongodb.ReadPreference) DataAccessException(org.springframework.dao.DataAccessException) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) MongoClient(com.mongodb.client.MongoClient) BulkMode(org.springframework.data.mongodb.core.BulkOperations.BulkMode) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) MongoConverter(org.springframework.data.mongodb.core.convert.MongoConverter) Distance(org.springframework.data.geo.Distance) IndexOperations(org.springframework.data.mongodb.core.index.IndexOperations) EntityReader(org.springframework.data.convert.EntityReader) ArrayFilter(org.springframework.data.mongodb.core.query.UpdateDefinition.ArrayFilter) MongoCursor(com.mongodb.client.MongoCursor) Granularity(org.springframework.data.mongodb.core.timeseries.Granularity) MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) AggregationDefinition(org.springframework.data.mongodb.core.QueryOperations.AggregationDefinition) GroupBy(org.springframework.data.mongodb.core.mapreduce.GroupBy) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) AggregationOptions(org.springframework.data.mongodb.core.aggregation.AggregationOptions) ApplicationContext(org.springframework.context.ApplicationContext) Query(org.springframework.data.mongodb.core.query.Query) TimeUnit(java.util.concurrent.TimeUnit) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) ClientSessionOptions(com.mongodb.ClientSessionOptions) GeoResults(org.springframework.data.geo.GeoResults) DeleteResult(com.mongodb.client.result.DeleteResult) Log(org.apache.commons.logging.Log) ClientSession(com.mongodb.client.ClientSession) GeoResult(org.springframework.data.geo.GeoResult) SessionSynchronization(org.springframework.data.mongodb.SessionSynchronization) WriteConcern(com.mongodb.WriteConcern) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) AggregationOptions(org.springframework.data.mongodb.core.aggregation.AggregationOptions) CloseableIterator(org.springframework.data.util.CloseableIterator) AggregationDefinition(org.springframework.data.mongodb.core.QueryOperations.AggregationDefinition) Document(org.bson.Document) Collation(org.springframework.data.mongodb.core.query.Collation) TypedAggregation(org.springframework.data.mongodb.core.aggregation.TypedAggregation) AggregateIterable(com.mongodb.client.AggregateIterable)

Example 7 with AggregationOptions

use of org.springframework.data.mongodb.core.aggregation.AggregationOptions in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateUnitTests method aggregateShouldHonorOptionsHint.

// DATAMONGO-1836
@Test
void aggregateShouldHonorOptionsHint() {
    Document hint = new Document("dummyHint", 1);
    AggregationOptions options = AggregationOptions.builder().hint(hint).build();
    template.aggregate(newAggregation(Sith.class, project("id")).withOptions(options), AutogenerateableId.class, Document.class).subscribe();
    verify(aggregatePublisher).hint(hint);
}
Also used : AggregationOptions(org.springframework.data.mongodb.core.aggregation.AggregationOptions) AutogenerateableId(org.springframework.data.mongodb.core.MongoTemplateUnitTests.AutogenerateableId) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Example 8 with AggregationOptions

use of org.springframework.data.mongodb.core.aggregation.AggregationOptions in project spring-data-mongodb by spring-projects.

the class ReactiveMongoTemplateUnitTests method aggregateShouldHonorOptionsComment.

// DATAMONGO-2153
@Test
void aggregateShouldHonorOptionsComment() {
    AggregationOptions options = AggregationOptions.builder().comment("expensive").build();
    template.aggregate(newAggregation(Sith.class, project("id")).withOptions(options), AutogenerateableId.class, Document.class).subscribe();
    verify(aggregatePublisher).comment("expensive");
}
Also used : AggregationOptions(org.springframework.data.mongodb.core.aggregation.AggregationOptions) AutogenerateableId(org.springframework.data.mongodb.core.MongoTemplateUnitTests.AutogenerateableId) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Example 9 with AggregationOptions

use of org.springframework.data.mongodb.core.aggregation.AggregationOptions in project spring-data-mongodb by spring-projects.

the class StringBasedAggregation method doExecute.

/*
	 * (non-Javascript)
	 * @see org.springframework.data.mongodb.repository.query.AbstractReactiveMongoQuery#doExecute(org.springframework.data.mongodb.repository.query.MongoQueryMethod, org.springframework.data.repository.query.ResultProcessor, org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor, java.lang.Class)
	 */
@Override
protected Object doExecute(MongoQueryMethod method, ResultProcessor resultProcessor, ConvertingParameterAccessor accessor, Class<?> typeToRead) {
    Class<?> sourceType = method.getDomainClass();
    Class<?> targetType = typeToRead;
    List<AggregationOperation> pipeline = computePipeline(method, accessor);
    AggregationUtils.appendSortIfPresent(pipeline, accessor, typeToRead);
    if (method.isSliceQuery()) {
        AggregationUtils.appendLimitAndOffsetIfPresent(pipeline, accessor, LongUnaryOperator.identity(), limit -> limit + 1);
    } else {
        AggregationUtils.appendLimitAndOffsetIfPresent(pipeline, accessor);
    }
    boolean isSimpleReturnType = isSimpleReturnType(typeToRead);
    boolean isRawAggregationResult = ClassUtils.isAssignable(AggregationResults.class, typeToRead);
    if (isSimpleReturnType) {
        targetType = Document.class;
    } else if (isRawAggregationResult) {
        // 🙈
        targetType = method.getReturnType().getRequiredActualType().getRequiredComponentType().getType();
    }
    AggregationOptions options = computeOptions(method, accessor);
    TypedAggregation<?> aggregation = new TypedAggregation<>(sourceType, pipeline, options);
    if (method.isStreamQuery()) {
        Stream<?> stream = mongoOperations.aggregateStream(aggregation, targetType).stream();
        if (isSimpleReturnType) {
            return stream.map(it -> AggregationUtils.extractSimpleTypeResult((Document) it, typeToRead, mongoConverter));
        }
        return stream;
    }
    AggregationResults<Object> result = (AggregationResults<Object>) mongoOperations.aggregate(aggregation, targetType);
    if (isRawAggregationResult) {
        return result;
    }
    List<Object> results = result.getMappedResults();
    if (method.isCollectionQuery()) {
        return isSimpleReturnType ? convertResults(typeToRead, results) : results;
    }
    if (method.isSliceQuery()) {
        Pageable pageable = accessor.getPageable();
        int pageSize = pageable.getPageSize();
        List<Object> resultsToUse = isSimpleReturnType ? convertResults(typeToRead, results) : results;
        boolean hasNext = resultsToUse.size() > pageSize;
        return new SliceImpl<>(hasNext ? resultsToUse.subList(0, pageSize) : resultsToUse, pageable, hasNext);
    }
    Object uniqueResult = result.getUniqueMappedResult();
    return isSimpleReturnType ? AggregationUtils.extractSimpleTypeResult((Document) uniqueResult, typeToRead, mongoConverter) : uniqueResult;
}
Also used : AggregationOptions(org.springframework.data.mongodb.core.aggregation.AggregationOptions) Document(org.bson.Document) SliceImpl(org.springframework.data.domain.SliceImpl) Pageable(org.springframework.data.domain.Pageable) AggregationResults(org.springframework.data.mongodb.core.aggregation.AggregationResults) TypedAggregation(org.springframework.data.mongodb.core.aggregation.TypedAggregation) AggregationOperation(org.springframework.data.mongodb.core.aggregation.AggregationOperation)

Example 10 with AggregationOptions

use of org.springframework.data.mongodb.core.aggregation.AggregationOptions in project spring-data-mongodb by spring-projects.

the class ReactiveStringBasedAggregationUnitTests method plainStringAggregationConsidersMeta.

// DATAMONGO-2153
@Test
public void plainStringAggregationConsidersMeta() {
    AggregationInvocation invocation = executeAggregation("plainStringAggregation");
    AggregationOptions options = invocation.aggregation.getOptions();
    assertThat(options.getComment()).contains("expensive-aggregation");
    assertThat(options.getCursorBatchSize()).isEqualTo(42);
}
Also used : AggregationOptions(org.springframework.data.mongodb.core.aggregation.AggregationOptions) Test(org.junit.jupiter.api.Test)

Aggregations

AggregationOptions (org.springframework.data.mongodb.core.aggregation.AggregationOptions)12 Document (org.bson.Document)7 Test (org.junit.jupiter.api.Test)6 TypedAggregation (org.springframework.data.mongodb.core.aggregation.TypedAggregation)4 AggregationResults (org.springframework.data.mongodb.core.aggregation.AggregationResults)3 AutogenerateableId (org.springframework.data.mongodb.core.MongoTemplateUnitTests.AutogenerateableId)2 ClientSessionOptions (com.mongodb.ClientSessionOptions)1 MongoException (com.mongodb.MongoException)1 ReadPreference (com.mongodb.ReadPreference)1 WriteConcern (com.mongodb.WriteConcern)1 AggregateIterable (com.mongodb.client.AggregateIterable)1 ClientSession (com.mongodb.client.ClientSession)1 DistinctIterable (com.mongodb.client.DistinctIterable)1 FindIterable (com.mongodb.client.FindIterable)1 MapReduceIterable (com.mongodb.client.MapReduceIterable)1 MongoClient (com.mongodb.client.MongoClient)1 MongoCollection (com.mongodb.client.MongoCollection)1 MongoCursor (com.mongodb.client.MongoCursor)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 MongoIterable (com.mongodb.client.MongoIterable)1