Search in sources :

Example 1 with GroupOperation

use of org.springframework.data.mongodb.core.aggregation.GroupOperation in project tutorials by eugenp.

the class ZipsAggregationLiveTest method whenStatesHavePopGrtrThan10MillionAndSorted_thenSuccess.

@Test
public void whenStatesHavePopGrtrThan10MillionAndSorted_thenSuccess() {
    GroupOperation groupByStateAndSumPop = group("state").sum("pop").as("statePop");
    MatchOperation filterStates = match(new Criteria("statePop").gt(10000000));
    SortOperation sortByPopDesc = sort(new Sort(Direction.DESC, "statePop"));
    Aggregation aggregation = newAggregation(groupByStateAndSumPop, filterStates, sortByPopDesc);
    AggregationResults<StatePopulation> result = mongoTemplate.aggregate(aggregation, "zips", StatePopulation.class);
    /*
         * Assert that all states have population
         * greater than 10000000
         */
    result.forEach(statePop -> {
        assertTrue(statePop.getStatePop() > 10000000);
    });
    /*
         * Assert that states fetched are in sorted by
         * decreasing population
         */
    List<StatePopulation> actualList = StreamSupport.stream(result.spliterator(), false).collect(Collectors.toList());
    List<StatePopulation> expectedList = new ArrayList<>(actualList);
    Collections.sort(expectedList, (sp1, sp2) -> sp2.getStatePop() - sp1.getStatePop());
    assertEquals(expectedList, actualList);
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) SortOperation(org.springframework.data.mongodb.core.aggregation.SortOperation) StatePopulation(org.baeldung.aggregation.model.StatePopulation) ArrayList(java.util.ArrayList) Sort(org.springframework.data.domain.Sort) Criteria(org.springframework.data.mongodb.core.query.Criteria) MatchOperation(org.springframework.data.mongodb.core.aggregation.MatchOperation) GroupOperation(org.springframework.data.mongodb.core.aggregation.GroupOperation) Test(org.junit.Test)

Example 2 with GroupOperation

use of org.springframework.data.mongodb.core.aggregation.GroupOperation in project tutorials by eugenp.

the class ZipsAggregationLiveTest method whenStateWithLowestAvgCityPopIsND_theSuccess.

@Test
public void whenStateWithLowestAvgCityPopIsND_theSuccess() {
    GroupOperation sumTotalCityPop = group("state", "city").sum("pop").as("cityPop");
    GroupOperation averageStatePop = group("_id.state").avg("cityPop").as("avgCityPop");
    SortOperation sortByAvgPopAsc = sort(new Sort(Direction.ASC, "avgCityPop"));
    ProjectionOperation projectToMatchModel = project().andExpression("_id").as("state").andExpression("avgCityPop").as("statePop");
    LimitOperation limitToOnlyFirstDoc = limit(1);
    Aggregation aggregation = newAggregation(sumTotalCityPop, averageStatePop, sortByAvgPopAsc, limitToOnlyFirstDoc, projectToMatchModel);
    AggregationResults<StatePopulation> result = mongoTemplate.aggregate(aggregation, "zips", StatePopulation.class);
    StatePopulation smallestState = result.getUniqueMappedResult();
    assertEquals("ND", smallestState.getState());
    assertTrue(smallestState.getStatePop().equals(1645));
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) ProjectionOperation(org.springframework.data.mongodb.core.aggregation.ProjectionOperation) SortOperation(org.springframework.data.mongodb.core.aggregation.SortOperation) LimitOperation(org.springframework.data.mongodb.core.aggregation.LimitOperation) StatePopulation(org.baeldung.aggregation.model.StatePopulation) Sort(org.springframework.data.domain.Sort) GroupOperation(org.springframework.data.mongodb.core.aggregation.GroupOperation) Test(org.junit.Test)

Example 3 with GroupOperation

use of org.springframework.data.mongodb.core.aggregation.GroupOperation in project tutorials by eugenp.

the class ZipsAggregationLiveTest method whenMaxTXAndMinDC_theSuccess.

@Test
public void whenMaxTXAndMinDC_theSuccess() {
    GroupOperation sumZips = group("state").count().as("zipCount");
    SortOperation sortByCount = sort(Direction.ASC, "zipCount");
    GroupOperation groupFirstAndLast = group().first("_id").as("minZipState").first("zipCount").as("minZipCount").last("_id").as("maxZipState").last("zipCount").as("maxZipCount");
    Aggregation aggregation = newAggregation(sumZips, sortByCount, groupFirstAndLast);
    AggregationResults<DBObject> result = mongoTemplate.aggregate(aggregation, "zips", DBObject.class);
    DBObject dbObject = result.getUniqueMappedResult();
    assertEquals("DC", dbObject.get("minZipState"));
    assertEquals(24, dbObject.get("minZipCount"));
    assertEquals("TX", dbObject.get("maxZipState"));
    assertEquals(1671, dbObject.get("maxZipCount"));
}
Also used : Aggregation.newAggregation(org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) SortOperation(org.springframework.data.mongodb.core.aggregation.SortOperation) DBObject(com.mongodb.DBObject) GroupOperation(org.springframework.data.mongodb.core.aggregation.GroupOperation) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)3 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)3 GroupOperation (org.springframework.data.mongodb.core.aggregation.GroupOperation)3 SortOperation (org.springframework.data.mongodb.core.aggregation.SortOperation)3 StatePopulation (org.baeldung.aggregation.model.StatePopulation)2 Sort (org.springframework.data.domain.Sort)2 DBObject (com.mongodb.DBObject)1 ArrayList (java.util.ArrayList)1 LimitOperation (org.springframework.data.mongodb.core.aggregation.LimitOperation)1 MatchOperation (org.springframework.data.mongodb.core.aggregation.MatchOperation)1 ProjectionOperation (org.springframework.data.mongodb.core.aggregation.ProjectionOperation)1 Criteria (org.springframework.data.mongodb.core.query.Criteria)1