Search in sources :

Example 81 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project ocvn by devgateway.

the class TotalCancelledTendersByYearController method totalCancelledTendersByYearByRationale.

@ApiOperation(value = "Total Cancelled tenders by year by cancel reason. " + "The tender amount is read from tender.value." + "The tender status has to be 'cancelled'. The year is retrieved from tender.tenderPeriod.startDate." + "The cancellation reason is read from tender.cancellationRationale.")
@RequestMapping(value = "/api/totalCancelledTendersByYearByRationale", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> totalCancelledTendersByYearByRationale(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project = new BasicDBObject();
    project.put(Fields.UNDERSCORE_ID, 0);
    addYearlyMonthlyProjection(filter, project, "$tender.tenderPeriod.startDate");
    project.put("tender.value.amount", 1);
    project.put("tender.cancellationRationale", 1);
    Aggregation agg = newAggregation(match(where("tender.status").is("cancelled").and("tender.tenderPeriod.startDate").exists(true).andOperator(getYearDefaultFilterCriteria(filter, "tender.tenderPeriod.startDate"))), new CustomProjectionOperation(project), group(getYearlyMonthlyGroupingFields(filter, "$tender.cancellationRationale")).sum("$tender.value.amount").as(Keys.TOTAL_CANCELLED_TENDERS_AMOUNT), getSortByYearMonth(filter));
    AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
    List<DBObject> list = results.getMappedResults();
    return list;
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BasicDBObject(com.mongodb.BasicDBObject) CustomProjectionOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 82 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project ocvn by devgateway.

the class TotalCancelledTendersByYearController method totalCancelledTendersByYear.

@ApiOperation(value = "Total Cancelled tenders by year. The tender amount is read from tender.value." + "The tender status has to be 'cancelled'. The year is retrieved from tender.tenderPeriod.startDate.")
@RequestMapping(value = "/api/totalCancelledTendersByYear", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> totalCancelledTendersByYear(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    DBObject project = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
    project.put("tender.value.amount", 1);
    Aggregation agg = newAggregation(match(where("tender.status").is("cancelled").and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), new CustomOperation(new BasicDBObject("$project", project)), getYearlyMonthlyGroupingOperation(filter).sum("$tender.value.amount").as(Keys.TOTAL_CANCELLED_TENDERS_AMOUNT), transformYearlyGrouping(filter).andInclude(Keys.TOTAL_CANCELLED_TENDERS_AMOUNT), getSortByYearMonth(filter));
    AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
    List<DBObject> list = results.getMappedResults();
    return list;
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) BasicDBObject(com.mongodb.BasicDBObject) CustomOperation(org.devgateway.toolkit.persistence.mongo.aggregate.CustomOperation) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 83 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project ocvn by devgateway.

the class VNImportService method getMaxTenderValue.

private BigDecimal getMaxTenderValue() {
    Aggregation agg = Aggregation.newAggregation(match(where("tender.value.amount").exists(true)), project().and("tender.value.amount").as("tender.value.amount"), group().max("tender.value.amount").as("maxTenderValue"), project().andInclude("maxTenderValue").andExclude(Fields.UNDERSCORE_ID));
    AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
    return results.getMappedResults().size() == 0 ? BigDecimal.ZERO : BigDecimal.valueOf((double) results.getMappedResults().get(0).get("maxTenderValue"));
}
Also used : Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) DBObject(com.mongodb.DBObject)

Example 84 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation in project ocvn by devgateway.

the class AbstractFlagReleaseSearchController method releaseFlagSearch.

@JsonView(Views.Internal.class)
public List<DBObject> releaseFlagSearch(@ModelAttribute @Valid final YearFilterPagingRequest filter) {
    Aggregation agg = newAggregation(match(where("flags.flaggedStats.0").exists(true).and(getFlagProperty()).is(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), unwind("flags.flaggedStats"), match(where(getFlagProperty()).is(true)), project("ocid", "tender.procuringEntity.name", "tender.tenderPeriod", "flags", "tender.title", "tag").and("tender.value").as("tender.value").and("awards.value").as("awards.value").andExclude(Fields.UNDERSCORE_ID), sort(Sort.Direction.DESC, "flags.flaggedStats.count"), skip(filter.getSkip()), limit(filter.getPageSize()));
    AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class);
    List<DBObject> list = results.getMappedResults();
    return list;
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) DBObject(com.mongodb.DBObject) JsonView(com.fasterxml.jackson.annotation.JsonView)

Example 85 with Aggregation

use of org.springframework.data.mongodb.core.aggregation.Aggregation 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!");
    AggregationOperationContext rootContext = context == null ? Aggregation.DEFAULT_CONTEXT : context;
    AggregationOptions options = aggregation.getOptions();
    List<Document> pipeline = aggregation.toPipeline(rootContext);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Streaming aggregation: {} in collection {}", 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()).useCursor(true);
        if (options.getCursorBatchSize() != null) {
            cursor = cursor.batchSize(options.getCursorBatchSize());
        }
        if (options.getCollation().isPresent()) {
            cursor = cursor.collation(options.getCollation().map(Collation::toMongoCollation).get());
        }
        return new CloseableIterableCursorAdapter<>(cursor.iterator(), exceptionTranslator, readCallback);
    });
}
Also used : Document(org.bson.Document) PropertyReferenceException(org.springframework.data.mapping.PropertyReferenceException) MongoDatabase(com.mongodb.client.MongoDatabase) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) Fields(org.springframework.data.mongodb.core.aggregation.Fields) ClientSession(com.mongodb.session.ClientSession) 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) Resource(org.springframework.core.io.Resource) AggregationOperationContext(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) ClassUtils(org.springframework.util.ClassUtils) NonNull(lombok.NonNull) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Mongo(com.mongodb.Mongo) AggregateIterable(com.mongodb.client.AggregateIterable) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) Cursor(com.mongodb.Cursor) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) CollectionUtils(org.springframework.util.CollectionUtils) PropertyPath(org.springframework.data.mapping.PropertyPath) Id(org.springframework.data.annotation.Id) SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory) ApplicationContextAware(org.springframework.context.ApplicationContextAware) AfterDeleteEvent(org.springframework.data.mongodb.core.mapping.event.AfterDeleteEvent) 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) GroupByResults(org.springframework.data.mongodb.core.mapreduce.GroupByResults) MappingContext(org.springframework.data.mapping.context.MappingContext) Bson(org.bson.conversions.Bson) ProjectionInformation(org.springframework.data.projection.ProjectionInformation) AccessLevel(lombok.AccessLevel) MappingException(org.springframework.data.mapping.MappingException) ConvertingPropertyAccessor(org.springframework.data.mapping.model.ConvertingPropertyAccessor) Nullable(org.springframework.lang.Nullable) MongoSimpleTypes(org.springframework.data.mongodb.core.mapping.MongoSimpleTypes) com.mongodb.client.model(com.mongodb.client.model) ConversionService(org.springframework.core.convert.ConversionService) BeforeConvertEvent(org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent) AfterConvertEvent(org.springframework.data.mongodb.core.mapping.event.AfterConvertEvent) BeforeSaveEvent(org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent) ObjectUtils(org.springframework.util.ObjectUtils) 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) DBCursor(com.mongodb.DBCursor) MongoMappingEvent(org.springframework.data.mongodb.core.mapping.event.MongoMappingEvent) MongoIterable(com.mongodb.client.MongoIterable) org.springframework.data.mongodb.core.convert(org.springframework.data.mongodb.core.convert) JSONParseException(com.mongodb.util.JSONParseException) RequiredArgsConstructor(lombok.RequiredArgsConstructor) LoggerFactory(org.slf4j.LoggerFactory) CloseableIterator(org.springframework.data.util.CloseableIterator) Collation(org.springframework.data.mongodb.core.query.Collation) MongoClientVersion(org.springframework.data.mongodb.util.MongoClientVersion) BeforeDeleteEvent(org.springframework.data.mongodb.core.mapping.event.BeforeDeleteEvent) BsonValue(org.bson.BsonValue) PersistenceExceptionTranslator(org.springframework.dao.support.PersistenceExceptionTranslator) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) MapReduceOptions(org.springframework.data.mongodb.core.mapreduce.MapReduceOptions) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Pair(org.springframework.data.util.Pair) MapReduceIterable(com.mongodb.client.MapReduceIterable) Update(org.springframework.data.mongodb.core.query.Update) StreamUtils(org.springframework.data.util.StreamUtils) 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) Entry(java.util.Map.Entry) Codec(org.bson.codecs.Codec) TypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext) 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) BulkMode(org.springframework.data.mongodb.core.BulkOperations.BulkMode) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) Distance(org.springframework.data.geo.Distance) IndexOperations(org.springframework.data.mongodb.core.index.IndexOperations) EntityReader(org.springframework.data.convert.EntityReader) AfterLoadEvent(org.springframework.data.mongodb.core.mapping.event.AfterLoadEvent) MongoCursor(com.mongodb.client.MongoCursor) MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) GroupBy(org.springframework.data.mongodb.core.mapreduce.GroupBy) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) Logger(org.slf4j.Logger) AggregationOptions(org.springframework.data.mongodb.core.aggregation.AggregationOptions) ConnectionCallback(org.springframework.jca.cci.core.ConnectionCallback) DBCollection(com.mongodb.DBCollection) ApplicationContext(org.springframework.context.ApplicationContext) Query(org.springframework.data.mongodb.core.query.Query) TimeUnit(java.util.concurrent.TimeUnit) AfterSaveEvent(org.springframework.data.mongodb.core.mapping.event.AfterSaveEvent) ClientSessionOptions(com.mongodb.ClientSessionOptions) GeoResults(org.springframework.data.geo.GeoResults) MongoClient(com.mongodb.MongoClient) DeleteResult(com.mongodb.client.result.DeleteResult) AllArgsConstructor(lombok.AllArgsConstructor) GeoResult(org.springframework.data.geo.GeoResult) 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) AggregationOperationContext(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) TypeBasedAggregationOperationContext(org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext) Document(org.bson.Document) AggregateIterable(com.mongodb.client.AggregateIterable) Collation(org.springframework.data.mongodb.core.query.Collation)

Aggregations

Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)113 DBObject (com.mongodb.DBObject)79 ApiOperation (io.swagger.annotations.ApiOperation)79 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)79 BasicDBObject (com.mongodb.BasicDBObject)73 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)73 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)55 CustomGroupingOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomGroupingOperation)22 Test (org.junit.Test)20 CustomOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomOperation)16 Document (org.bson.Document)14 MongoVersion (org.springframework.data.mongodb.test.util.MongoVersion)5 JsonView (com.fasterxml.jackson.annotation.JsonView)4 Criteria (org.springframework.data.mongodb.core.query.Criteria)4 CustomUnwindOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomUnwindOperation)3 ChangeStreamDocument (com.mongodb.client.model.changestream.ChangeStreamDocument)2 FullDocument (com.mongodb.client.model.changestream.FullDocument)2 List (java.util.List)2 TypedAggregation (org.springframework.data.mongodb.core.aggregation.TypedAggregation)2 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)2