org.springframework.data.mongodb.core.aggregation.GroupOperation Java Examples
The following examples show how to use
org.springframework.data.mongodb.core.aggregation.GroupOperation.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ZipsAggregationLiveTest.java From tutorials with MIT License | 6 votes |
@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)); }
Example #2
Source File: ZipsAggregationLiveTest.java From tutorials with MIT License | 6 votes |
@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<Document> result = mongoTemplate.aggregate(aggregation, "zips", Document.class); Document document = result.getUniqueMappedResult(); assertEquals("DC", document.get("minZipState")); assertEquals(24, document.get("minZipCount")); assertEquals("TX", document.get("maxZipState")); assertEquals(1671, document.get("maxZipCount")); }
Example #3
Source File: UserInfoServiceImpl.java From ExecDashboard with Apache License 2.0 | 5 votes |
@Override public List<String> getFrequentExecutives() { List<String> executives = new LinkedList<>(); try { Map<String, Integer> resultsMap = new HashMap<>(); Map<String, Integer> resultsMapSorted = new LinkedHashMap<>(); List<String> views = new ArrayList<>(); views.add(PORTFOLIO); views.add(PORTFOLIOMETRIC); GroupOperation groupByExecutiveViewId = Aggregation.group(EXECUTIVEVIEWID).count().as(TOTAL); MatchOperation filter = Aggregation.match(new Criteria(VIEW).in(views).andOperator( Criteria.where(TIMESTAMP).gte(getTimeStamp(30)), Criteria.where(EXECUTIVEVIEWID).ne(null))); Aggregation aggregation = Aggregation.newAggregation(filter, groupByExecutiveViewId); AggregationResults<DBObject> temp = mongoTemplate.aggregate(aggregation, TRACKVIEWS, DBObject.class); if (temp != null) { List<DBObject> results = temp.getMappedResults(); if (results != null && !results.isEmpty()) { for (DBObject object : results) { if (object.get(FIRSTORDER) != null) { String id = object.get(FIRSTORDER).toString(); Integer total = (Integer) object.get(TOTAL); resultsMap.put(id, total); } } resultsMap.entrySet().stream().sorted(Map.Entry.<String, Integer> comparingByValue().reversed()) .forEachOrdered(x -> resultsMapSorted.put(x.getKey(), x.getValue())); resultsMapSorted.forEach((eid, v) -> { ExecutiveSummaryList executive = executiveSummaryListRepository.findByEid(eid); if (executive != null) { executives.add(executive.getFirstName() + ", " + executive.getLastName()); } }); } } } catch (Exception e) { LOG.error("User Tracking, getFrequentExecutives :: " + e); } return executives; }
Example #4
Source File: UserInfoServiceImpl.java From ExecDashboard with Apache License 2.0 | 5 votes |
@Override public List<String> getFrequentApplications() { List<String> applications = new LinkedList<>(); try { Map<String, Integer> resultsMap = new HashMap<>(); Map<String, Integer> resultsMapSorted = new LinkedHashMap<>(); List<String> views = new ArrayList<>(); views.add(PRODUCTMETRIC); views.add(PRODUCT); GroupOperation groupByApplicationViewId = Aggregation.group(APPLICATIONVIEWID).count().as(TOTAL); MatchOperation filter = Aggregation.match(new Criteria(VIEW).in(views).andOperator( Criteria.where(TIMESTAMP).gte(getTimeStamp(30)), Criteria.where(APPLICATIONVIEWID).ne(null))); Aggregation aggregation = Aggregation.newAggregation(filter, groupByApplicationViewId); AggregationResults<DBObject> temp = mongoTemplate.aggregate(aggregation, TRACKVIEWS, DBObject.class); if (temp != null) { List<DBObject> results = temp.getMappedResults(); if (results != null && !results.isEmpty()) { for (DBObject object : results) { if (object.get(FIRSTORDER) != null) { String id = object.get(FIRSTORDER).toString(); Integer total = (Integer) object.get(TOTAL); resultsMap.put(id, total); } } resultsMap.entrySet().stream().sorted(Map.Entry.<String, Integer> comparingByValue().reversed()) .forEachOrdered(x -> resultsMapSorted.put(x.getKey(), x.getValue())); resultsMapSorted.forEach((appId, v) -> { ApplicationDetails app = applicationDetailsRepository.findByAppId(appId); if (app != null) { applications.add(app.getAppName() + " - " + app.getLob()); } }); } } } catch (Exception e) { LOG.error("User Tracking, getFrequentApplications :: " + e); } return applications; }
Example #5
Source File: UserInfoServiceImpl.java From ExecDashboard with Apache License 2.0 | 5 votes |
@Override public List<String> getFrequentCards() { List<String> metrics = new LinkedList<>(); try { Map<String, Integer> resultsMap = new HashMap<>(); Map<String, Integer> resultsMapSorted = new LinkedHashMap<>(); List<String> views = new ArrayList<>(); views.add(PRODUCTMETRIC); views.add(PORTFOLIOMETRIC); GroupOperation groupByApplicationViewId = Aggregation.group(METRICSNAME).count().as(TOTAL); MatchOperation filter = Aggregation.match(new Criteria(VIEW).in(views).andOperator( Criteria.where(TIMESTAMP).gte(getTimeStamp(30)), Criteria.where(METRICSNAME).ne(null))); Aggregation aggregation = Aggregation.newAggregation(filter, groupByApplicationViewId); AggregationResults<DBObject> temp = mongoTemplate.aggregate(aggregation, TRACKVIEWS, DBObject.class); if (temp != null) { List<DBObject> results = temp.getMappedResults(); if (results != null && !results.isEmpty()) { for (DBObject object : results) { if (object.get(ID) != null) { String id = object.get(ID).toString(); Integer total = (Integer) object.get(TOTAL); resultsMap.put(id, total); } } resultsMap.entrySet().stream().sorted(Map.Entry.<String, Integer> comparingByValue().reversed()) .forEachOrdered(x -> resultsMapSorted.put(x.getKey(), x.getValue())); resultsMapSorted.forEach((k, v) -> metrics.add(k)); } } } catch (Exception e) { LOG.error("User Tracking, getFrequentCards :: " + e); } return metrics; }
Example #6
Source File: ZipsAggregationLiveTest.java From tutorials with MIT License | 5 votes |
@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); }
Example #7
Source File: SprintRepositoryImpl.java From mirrorgate with Apache License 2.0 | 4 votes |
private static GroupOperation firstIssueFields(final GroupOperation operation) { return ISSUE_FIELDS.keySet().stream() .reduce(operation, (o, s) -> o.first(s).as(s), (old, o) -> o); }
Example #8
Source File: SprintRepositoryImpl.java From mirrorgate with Apache License 2.0 | 4 votes |
private static GroupOperation firstSprintFields(final GroupOperation operation) { return SPRINT_FIELDS.keySet().stream() .reduce(operation, (o, s) -> o.first(s).as(SPRINT_FIELDS.get(s)), (old, o) -> o); }
Example #9
Source File: DashboardRepositoryImpl.java From mirrorgate with Apache License 2.0 | 4 votes |
private static GroupOperation firstDashboardFields(final GroupOperation operation) { return DASHBOARD_FIELDS.keySet().stream() .reduce(operation, (o, s) -> o.first(s).as(s), (old, o) -> o); }