Search in sources :

Example 1 with MatchOperation

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

use of org.springframework.data.mongodb.core.aggregation.MatchOperation in project commons-dao by reportportal.

the class AggregationUtilsTest method matchOperationFromFilter.

@Test
public void matchOperationFromFilter() {
    String expected = "{ \"aggregate\" : \"__collection__\" , \"pipeline\" : [ { \"$match\" : { \"$and\" : [ { \"status\" : { \"$ne\" : \"IN_PROGRESS\"}}]}}]}";
    Filter filter = Filter.builder().withTarget(Launch.class).withCondition(FilterConditionUtils.LAUNCH_NOT_IN_PROGRESS()).build();
    MatchOperation matchOperation = AggregationUtils.matchOperationFromFilter(filter, mongoOperations, Launch.class);
    Assert.assertEquals(expected, newAggregation(matchOperation).toString());
}
Also used : Filter(com.epam.ta.reportportal.database.search.Filter) MatchOperation(org.springframework.data.mongodb.core.aggregation.MatchOperation) BaseDaoTest(com.epam.ta.reportportal.BaseDaoTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 MatchOperation (org.springframework.data.mongodb.core.aggregation.MatchOperation)2 BaseDaoTest (com.epam.ta.reportportal.BaseDaoTest)1 Filter (com.epam.ta.reportportal.database.search.Filter)1 ArrayList (java.util.ArrayList)1 StatePopulation (org.baeldung.aggregation.model.StatePopulation)1 Sort (org.springframework.data.domain.Sort)1 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)1 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)1 GroupOperation (org.springframework.data.mongodb.core.aggregation.GroupOperation)1 SortOperation (org.springframework.data.mongodb.core.aggregation.SortOperation)1 Criteria (org.springframework.data.mongodb.core.query.Criteria)1