Search in sources :

Example 1 with AggregationOperationContext

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

the class MongoTemplate method aggregate.

protected <O> AggregationResults<O> aggregate(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!");
    AggregationOperationContext rootContext = context == null ? Aggregation.DEFAULT_CONTEXT : context;
    return doAggregate(aggregation, collectionName, outputType, rootContext);
}
Also used : AggregationOperationContext(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) TypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext)

Example 2 with AggregationOperationContext

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

the class MongoTemplate method aggregate.

/* (non-Javadoc)
	 * @see org.springframework.data.mongodb.core.MongoOperations#aggregate(org.springframework.data.mongodb.core.aggregation.TypedAggregation, java.lang.String, java.lang.Class)
	 */
@Override
public <O> AggregationResults<O> aggregate(TypedAggregation<?> aggregation, String inputCollectionName, Class<O> outputType) {
    Assert.notNull(aggregation, "Aggregation pipeline must not be null!");
    AggregationOperationContext context = new TypeBasedAggregationOperationContext(aggregation.getInputType(), mappingContext, queryMapper);
    return aggregate(aggregation, inputCollectionName, outputType, context);
}
Also used : AggregationOperationContext(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) TypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext) TypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext)

Example 3 with AggregationOperationContext

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

the class ReactiveMongoTemplate method aggregate.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.core.ReactiveMongoOperations#aggregate(org.springframework.data.mongodb.core.aggregation.TypedAggregation, java.lang.String, java.lang.Class)
	 */
@Override
public <O> Flux<O> aggregate(TypedAggregation<?> aggregation, String inputCollectionName, Class<O> outputType) {
    Assert.notNull(aggregation, "Aggregation pipeline must not be null!");
    AggregationOperationContext context = new TypeBasedAggregationOperationContext(aggregation.getInputType(), mappingContext, queryMapper);
    return aggregate(aggregation, inputCollectionName, outputType, context);
}
Also used : AggregationOperationContext(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) PrefixingDelegatingAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.PrefixingDelegatingAggregationOperationContext) TypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext) TypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext)

Example 4 with AggregationOperationContext

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

the class ReactiveMongoTemplate method aggregate.

/**
 * @param aggregation must not be {@literal null}.
 * @param collectionName must not be {@literal null}.
 * @param outputType must not be {@literal null}.
 * @param context can be {@literal null} and will be defaulted to {@link Aggregation#DEFAULT_CONTEXT}.
 * @return never {@literal null}.
 */
protected <O> Flux<O> aggregate(Aggregation aggregation, String collectionName, Class<O> outputType, @Nullable AggregationOperationContext context) {
    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!");
    AggregationOperationContext rootContext = context == null ? Aggregation.DEFAULT_CONTEXT : context;
    AggregationOptions options = aggregation.getOptions();
    List<Document> pipeline = aggregation.toPipeline(rootContext);
    Assert.isTrue(!options.isExplain(), "Cannot use explain option with streaming!");
    Assert.isNull(options.getCursorBatchSize(), "Cannot use batchSize cursor option with streaming!");
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Streaming aggregation: {} in collection {}", serializeToJsonSafely(pipeline), collectionName);
    }
    ReadDocumentCallback<O> readCallback = new ReadDocumentCallback<>(mongoConverter, outputType, collectionName);
    return execute(collectionName, collection -> aggregateAndMap(collection, pipeline, options, readCallback));
}
Also used : AggregationOptions(org.springframework.data.mongodb.core.aggregation.AggregationOptions) AggregationOperationContext(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) PrefixingDelegatingAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.PrefixingDelegatingAggregationOperationContext) TypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext) Document(org.bson.Document) FullDocument(com.mongodb.client.model.changestream.FullDocument) ReturnDocument(com.mongodb.client.model.ReturnDocument)

Example 5 with AggregationOperationContext

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

the class ChangeStreamTask method prepareFilter.

List<Document> prepareFilter(MongoTemplate template, ChangeStreamOptions options) {
    if (!options.getFilter().isPresent()) {
        return Collections.emptyList();
    }
    Object filter = options.getFilter().get();
    if (filter instanceof Aggregation) {
        Aggregation agg = (Aggregation) filter;
        AggregationOperationContext context = agg instanceof TypedAggregation ? new TypeBasedAggregationOperationContext(((TypedAggregation<?>) agg).getInputType(), template.getConverter().getMappingContext(), queryMapper) : Aggregation.DEFAULT_CONTEXT;
        return agg.toPipeline(new PrefixingDelegatingAggregationOperationContext(context, "fullDocument", blacklist));
    } else if (filter instanceof List) {
        return (List<Document>) filter;
    } else {
        throw new IllegalArgumentException("ChangeStreamRequestOptions.filter mut be either an Aggregation or a plain list of Documents");
    }
}
Also used : TypedAggregation(org.springframework.data.mongodb.core.aggregation.TypedAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) PrefixingDelegatingAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.PrefixingDelegatingAggregationOperationContext) PrefixingDelegatingAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.PrefixingDelegatingAggregationOperationContext) AggregationOperationContext(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) TypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext) TypedAggregation(org.springframework.data.mongodb.core.aggregation.TypedAggregation) List(java.util.List) Document(org.bson.Document) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) BsonDocument(org.bson.BsonDocument) FullDocument(com.mongodb.client.model.changestream.FullDocument) TypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext)

Aggregations

AggregationOperationContext (org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)8 TypeBasedAggregationOperationContext (org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext)8 Document (org.bson.Document)3 PrefixingDelegatingAggregationOperationContext (org.springframework.data.mongodb.core.aggregation.PrefixingDelegatingAggregationOperationContext)3 TypedAggregation (org.springframework.data.mongodb.core.aggregation.TypedAggregation)3 FullDocument (com.mongodb.client.model.changestream.FullDocument)2 ClientSessionOptions (com.mongodb.ClientSessionOptions)1 Cursor (com.mongodb.Cursor)1 DBCollection (com.mongodb.DBCollection)1 DBCursor (com.mongodb.DBCursor)1 Mongo (com.mongodb.Mongo)1 MongoClient (com.mongodb.MongoClient)1 MongoException (com.mongodb.MongoException)1 ReadPreference (com.mongodb.ReadPreference)1 WriteConcern (com.mongodb.WriteConcern)1 AggregateIterable (com.mongodb.client.AggregateIterable)1 DistinctIterable (com.mongodb.client.DistinctIterable)1 FindIterable (com.mongodb.client.FindIterable)1 MapReduceIterable (com.mongodb.client.MapReduceIterable)1 MongoCollection (com.mongodb.client.MongoCollection)1