Search in sources :

Example 56 with Aggregation

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

the class MongoTemplateUnitTests method aggregateShouldUseCollationWhenPresent.

// DATAMONGO-1518, DATAMONGO-1824
@Test
public void aggregateShouldUseCollationWhenPresent() {
    Aggregation aggregation = newAggregation(project("id")).withOptions(newAggregationOptions().collation(Collation.of("fr")).build());
    template.aggregate(aggregation, AutogenerateableId.class, Document.class);
    verify(aggregateIterable).collation(eq(com.mongodb.client.model.Collation.builder().locale("fr").build()));
}
Also used : Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) Test(org.junit.Test)

Example 57 with Aggregation

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

the class MongoTemplateUnitTests method aggregateShouldUseBatchSizeWhenPresent.

// DATAMONGO-1824
@Test
public void aggregateShouldUseBatchSizeWhenPresent() {
    Aggregation aggregation = newAggregation(project("id")).withOptions(newAggregationOptions().collation(Collation.of("fr")).cursorBatchSize(100).build());
    template.aggregate(aggregation, AutogenerateableId.class, Document.class);
    verify(aggregateIterable).batchSize(100);
}
Also used : Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) Test(org.junit.Test)

Example 58 with Aggregation

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

the class CorruptionRiskDashboardIndicatorsStatsController method totalProjectsByYear.

@ApiOperation(value = "Count total projects by year/month.")
@RequestMapping(value = "/api/totalProjectsByYear", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
public List<DBObject> totalProjectsByYear(final YearFilterPagingRequest filter) {
    DBObject project1 = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project1, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
    project1.put(Fields.UNDERSCORE_ID, 0);
    Aggregation agg = newAggregation(match(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), new CustomProjectionOperation(project1), group(getYearlyMonthlyGroupingFields(filter)).count().as(Keys.PROJECT_COUNT), transformYearlyGrouping(filter).andInclude(Keys.PROJECT_COUNT), 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) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 59 with Aggregation

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

the class CorruptionRiskDashboardIndicatorsStatsController method totalIndicatorsByIndicatorType.

private List<DBObject> totalIndicatorsByIndicatorType(String statsProperty, final YearFilterPagingRequest filter) {
    Aggregation agg = newAggregation(match(where("flags." + statsProperty + ".0").exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), unwind("flags." + statsProperty), project("flags." + statsProperty), group(statsProperty + ".type").sum(statsProperty + ".count").as(Keys.INDICATOR_COUNT), project(Keys.INDICATOR_COUNT).and(Fields.UNDERSCORE_ID).as(Keys.TYPE).andExclude(Fields.UNDERSCORE_ID), sort(Sort.Direction.ASC, "type"));
    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) BasicDBObject(com.mongodb.BasicDBObject)

Example 60 with Aggregation

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

the class CorruptionRiskDashboardIndicatorsStatsController method totalProjectsByIndicatorTypeByYear.

private List<DBObject> totalProjectsByIndicatorTypeByYear(String statsProperty, final YearFilterPagingRequest filter) {
    DBObject project1 = new BasicDBObject();
    addYearlyMonthlyProjection(filter, project1, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF);
    project1.put("stats", "$flags." + statsProperty);
    project1.put(Fields.UNDERSCORE_ID, 0);
    Aggregation agg = newAggregation(match(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true).and("flags." + statsProperty + ".0").exists(true).andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), unwind("flags." + statsProperty), new CustomProjectionOperation(project1), group(getYearlyMonthlyGroupingFields(filter, "stats.type")).sum("stats.count").as(statsProperty.equals(ELIGIBLE_STATS) ? Keys.ELIGIBLE_COUNT : Keys.FLAGGED_COUNT).count().as(statsProperty.equals(ELIGIBLE_STATS) ? Keys.ELIGIBLE_PROJECT_COUNT : Keys.FLAGGED_PROJECT_COUNT), getSortByYearMonthWhenOtherGroups(filter, "_id.type"));
    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) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

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