Search in sources :

Example 6 with TypedAggregation

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

the class ReactiveMongoTemplate method changeStream.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#tail(org.springframework.data.mongodb.core.aggregation.Aggregation, java.lang.Class, org.springframework.data.mongodb.core.ChangeStreamOptions, java.lang.String)
	 */
@Override
public <T> Flux<ChangeStreamEvent<T>> changeStream(@Nullable Aggregation filter, Class<T> resultType, ChangeStreamOptions options, String collectionName) {
    Assert.notNull(resultType, "Result type must not be null!");
    Assert.notNull(options, "ChangeStreamOptions must not be null!");
    Assert.hasText(collectionName, "Collection name must not be null or empty!");
    if (filter == null) {
        return changeStream(Collections.emptyList(), resultType, options, collectionName);
    }
    AggregationOperationContext context = filter instanceof TypedAggregation ? new TypeBasedAggregationOperationContext(((TypedAggregation) filter).getInputType(), mappingContext, queryMapper) : Aggregation.DEFAULT_CONTEXT;
    return changeStream(filter.toPipeline(new PrefixingDelegatingAggregationOperationContext(context, "fullDocument", Arrays.asList("operationType", "fullDocument", "documentKey", "updateDescription", "ns"))), resultType, options, collectionName);
}
Also used : PrefixingDelegatingAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.PrefixingDelegatingAggregationOperationContext) AggregationOperationContext(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) PrefixingDelegatingAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.PrefixingDelegatingAggregationOperationContext) TypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext) TypedAggregation(org.springframework.data.mongodb.core.aggregation.TypedAggregation) TypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext)

Example 7 with TypedAggregation

use of org.springframework.data.mongodb.core.aggregation.TypedAggregation 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 8 with TypedAggregation

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

the class ReactiveChangeStreamOperationSupportUnitTests method listenWithDomainTypeCreatesTypedAggregation.

// DATAMONGO-2089
@Test
void listenWithDomainTypeCreatesTypedAggregation() {
    Criteria criteria = where("operationType").is("insert");
    changeStreamSupport.changeStream(Person.class).filter(criteria).listen().subscribe();
    ArgumentCaptor<ChangeStreamOptions> optionsArgumentCaptor = ArgumentCaptor.forClass(ChangeStreamOptions.class);
    verify(template).changeStream(isNull(), optionsArgumentCaptor.capture(), eq(Person.class));
    assertThat(optionsArgumentCaptor.getValue().getFilter()).hasValueSatisfying(it -> {
        assertThat(it).isInstanceOf(TypedAggregation.class);
        TypedAggregation<?> aggregation = (TypedAggregation<?>) it;
        assertThat(aggregation.getInputType()).isEqualTo(Person.class);
        assertThat(extractPipeline(aggregation)).containsExactly(new Document("$match", new Document("operationType", "insert")));
    });
}
Also used : TypedAggregation(org.springframework.data.mongodb.core.aggregation.TypedAggregation) Criteria(org.springframework.data.mongodb.core.query.Criteria) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Example 9 with TypedAggregation

use of org.springframework.data.mongodb.core.aggregation.TypedAggregation 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)

Aggregations

TypedAggregation (org.springframework.data.mongodb.core.aggregation.TypedAggregation)9 Document (org.bson.Document)5 TypeBasedAggregationOperationContext (org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext)5 AggregationOperationContext (org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)4 AggregationOptions (org.springframework.data.mongodb.core.aggregation.AggregationOptions)4 List (java.util.List)3 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)3 AggregationResults (org.springframework.data.mongodb.core.aggregation.AggregationResults)3 PrefixingDelegatingAggregationOperationContext (org.springframework.data.mongodb.core.aggregation.PrefixingDelegatingAggregationOperationContext)2 RelaxedTypeBasedAggregationOperationContext (org.springframework.data.mongodb.core.aggregation.RelaxedTypeBasedAggregationOperationContext)2 Criteria (org.springframework.data.mongodb.core.query.Criteria)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