Java Code Examples for org.springframework.data.mongodb.core.aggregation.Aggregation#newAggregation()
The following examples show how to use
org.springframework.data.mongodb.core.aggregation.Aggregation#newAggregation() .
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: GalleryOnListTest.java From jakduk-api with MIT License | 10 votes |
@Test public void findById() { ArrayList<ObjectId> arrTemp = new ArrayList<ObjectId>(); arrTemp.add(new ObjectId("54c4df893d96600d7f55a048")); arrTemp.add(new ObjectId("54c4e4833d96deb0f8592907")); AggregationOperation match = Aggregation.match(Criteria.where("_id").in(arrTemp)); //AggregationOperation group = Aggregation.group("article").count().as("count"); AggregationOperation sort = Aggregation.sort(Direction.ASC, "_id"); //AggregationOperation limit = Aggregation.limit(Constants.BOARD_LINE_NUMBER); Aggregation aggregation = Aggregation.newAggregation(match, /*group, */ sort /*, limit*/); AggregationResults<Gallery> results = mongoTemplate.aggregate(aggregation, "gallery", Gallery.class); System.out.println("findOneById=" + results.getMappedResults()); }
Example 2
Source File: ArticleCommentRepositoryImpl.java From jakduk-api with MIT License | 8 votes |
/** * boardItem의 boardId 기준 이상의 댓글 수를 가져온다 * * @param boardId 기준이 되는 boardItem의 boardId */ @Override public List<CommonCount> findCommentsCountGreaterThanBoardIdAndBoard(ObjectId boardId, Constants.BOARD_TYPE board) { AggregationOperation match1 = Aggregation.match(Criteria.where("article._id").gt(boardId).and("article.board").is(board.name())); AggregationOperation group = Aggregation.group("article").count().as("count"); AggregationOperation sort = Aggregation.sort(Sort.Direction.DESC, "count"); //AggregationOperation limit = Aggregation.limit(Constants.BOARD_TOP_LIMIT); Aggregation aggregation = Aggregation.newAggregation(match1, group, sort/*, limit*/); AggregationResults<CommonCount> results = mongoTemplate.aggregate(aggregation, Constants.COLLECTION_ARTICLE_COMMENT, CommonCount.class); return results.getMappedResults(); }
Example 3
Source File: ArticleCommentRepositoryImpl.java From jakduk-api with MIT License | 7 votes |
/** * 게시물 ID 에 해당하는 댓글 수를 가져온다. */ @Override public List<CommonCount> findCommentsCountByIds(List<ObjectId> ids) { AggregationOperation match = Aggregation.match(Criteria.where("article._id").in(ids)); AggregationOperation group = Aggregation.group("article").count().as("count"); //AggregationOperation sort = Aggregation.sort(Direction.ASC, "_id"); //AggregationOperation limit = Aggregation.limit(Constants.BOARD_LINE_NUMBER); Aggregation aggregation = Aggregation.newAggregation(match, group/*, sort, limit*/); AggregationResults<CommonCount> results = mongoTemplate.aggregate(aggregation, Constants.COLLECTION_ARTICLE_COMMENT, CommonCount.class); return results.getMappedResults(); }
Example 4
Source File: JakdukDAO.java From jakduk-api with MIT License | 7 votes |
public List<SupporterCount> getSupportFCCount(String language) { AggregationOperation match = Aggregation.match(Criteria.where("supportFC").exists(true)); AggregationOperation group = Aggregation.group("supportFC").count().as("count"); AggregationOperation project = Aggregation.project("count").and("_id").as("supportFC"); AggregationOperation sort = Aggregation.sort(Direction.DESC, "count"); Aggregation aggregation = Aggregation.newAggregation(match, group, project, sort); AggregationResults<SupporterCount> results = mongoTemplate.aggregate(aggregation, "user", SupporterCount.class); List<SupporterCount> users = results.getMappedResults(); for (SupporterCount supporterCount : users) { supporterCount.getSupportFC().getNames().removeIf(fcName -> !fcName.getLanguage().equals(language)); } return users; }
Example 5
Source File: TracerServiceImpl.java From biliob_backend with MIT License | 7 votes |
private void getCrawlCountAggregationData(Map<String, Object> resultMap) { Aggregation crawlCountAggregation = Aggregation.newAggregation( Aggregation.match(Criteria.where("class_name").is("SpiderTask")), Aggregation.group("class_name").sum("crawl_count").as("sumCrawlCount")); AggregationResults<Map> crawlCountAggregationResult = mongoTemplate.aggregate(crawlCountAggregation, "tracer", Map.class); resultMap.putAll(Objects.requireNonNull(crawlCountAggregationResult.getUniqueMappedResult())); }
Example 6
Source File: MySpringBootApplicationTests.java From spring-boot-101 with Apache License 2.0 | 7 votes |
@Test public void mongoAggregationTest() throws JsonProcessingException{ Criteria c = new Criteria(); c.and("sex").is("F"); Aggregation aggr = Aggregation.newAggregation( Aggregation.match(c), Aggregation.group("lastName").count().as("count") ); AggregationResults<BasicDBObject> aggrResult = mongoTemplate.aggregate(aggr, "person", BasicDBObject.class); if(!aggrResult.getMappedResults().isEmpty()){ for(BasicDBObject obj : aggrResult.getMappedResults()){ logger.info("count by first name: {}", objectMapper.writeValueAsString(obj)); } } }
Example 7
Source File: VideoServiceImpl.java From biliob_backend with MIT License | 7 votes |
/** * get top online video * * @return top online video */ @Override public Map getTopOnlineVideo() { Aggregation a = Aggregation.newAggregation( Aggregation.unwind("data"), Aggregation.sort(Sort.Direction.DESC, "data.datetime"), Aggregation.project("author", "title", "pic", "data"), Aggregation.limit(20)); List<Map> l = mongoTemplate.aggregate(a, "video_online", Map.class).getMappedResults(); ArrayList<Map> arrayList = new ArrayList<>(); arrayList.addAll(l); arrayList.sort( (aMap, bMap) -> { Map aData = (Map) aMap.get("data"); Integer aNumber = Integer.valueOf((String) aData.get("number")); Map bData = (Map) bMap.get("data"); Integer bNumber = Integer.valueOf((String) bData.get("number")); return bNumber - aNumber; }); return arrayList.get(0); }
Example 8
Source File: SiteServiceImpl.java From biliob_backend with MIT License | 7 votes |
public Map<Integer, List<Site>> listHistoryOnline(Integer days) { Integer[] groups = {0, 7, 28, 84, 252}; Map<Integer, List<Site>> result = new HashMap<>(); for (Integer delta : groups ) { Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, -delta); Date endDate = c.getTime(); c.add(Calendar.DATE, -1); Date startDate = c.getTime(); Aggregation ao = Aggregation.newAggregation( Aggregation.match(Criteria.where("datetime").gt(startDate).lte(endDate)) ); List<Site> data = mongoTemplate.aggregate(ao, Site.class, Site.class).getMappedResults(); result.put(delta, data); } return result; }
Example 9
Source File: VideoServiceImpl.java From biliob_backend with MIT License | 7 votes |
/** * Get top online video in one day. * * @return top online video response. */ @Override public ResponseEntity listOnlineVideo() { Calendar todayStart = Calendar.getInstance(); todayStart.add(Calendar.DATE, -1); Aggregation aggregation = Aggregation.newAggregation( Aggregation.match(Criteria.where("data.datetime").gt(todayStart.getTime())), Aggregation.limit(20), Aggregation.project("title", "author") .and("data") .filter( "item", ComparisonOperators.Gte.valueOf("$$item.datetime") .greaterThanEqualToValue(todayStart.getTime())) .as("$data")); AggregationResults<VideoOnline> aggregationResults = mongoTemplate.aggregate(aggregation, "video_online", VideoOnline.class); List<VideoOnline> videoOnlineList = aggregationResults.getMappedResults(); return new ResponseEntity<>(videoOnlineList, HttpStatus.OK); }
Example 10
Source File: ArticleCommentRepositoryImpl.java From jakduk-api with MIT License | 6 votes |
/** * 기준 ArticleComment ID 이상의 ArticleComment 목록을 가져온다. */ @Override public List<ArticleComment> findCommentsGreaterThanId(ObjectId objectId, Integer limit) { AggregationOperation match1 = Aggregation.match(Criteria.where("_id").gt(objectId)); AggregationOperation sort = Aggregation.sort(Sort.Direction.ASC, "_id"); AggregationOperation limit1 = Aggregation.limit(limit); Aggregation aggregation; if (! ObjectUtils.isEmpty(objectId)) { aggregation = Aggregation.newAggregation(match1, sort, limit1); } else { aggregation = Aggregation.newAggregation(sort, limit1); } AggregationResults<ArticleComment> results = mongoTemplate.aggregate(aggregation, Constants.COLLECTION_ARTICLE_COMMENT, ArticleComment.class); return results.getMappedResults(); }
Example 11
Source File: JakdukDAO.java From jakduk-api with MIT License | 6 votes |
public List<JakduComment> getJakduComments(String jakduScheduleId, ObjectId commentId) { AggregationOperation match1 = Aggregation.match(Criteria.where("jakduScheduleId").is(jakduScheduleId)); AggregationOperation match2 = Aggregation.match(Criteria.where("_id").gt(commentId)); AggregationOperation sort = Aggregation.sort(Direction.ASC, "_id"); AggregationOperation limit = Aggregation.limit(Constants.COMMENT_MAX_LIMIT); Aggregation aggregation; if (Objects.nonNull(commentId)) { aggregation = Aggregation.newAggregation(match1, match2, sort, limit); } else { aggregation = Aggregation.newAggregation(match1, sort, limit); } AggregationResults<JakduComment> results = mongoTemplate.aggregate(aggregation, "jakduComment", JakduComment.class); List<JakduComment> comments = results.getMappedResults(); return comments; }
Example 12
Source File: VideoServiceImpl.java From biliob_backend with MIT License | 5 votes |
@Override public Video getAggregatedData(Long aid) { Aggregation a = Aggregation.newAggregation( Aggregation.match(Criteria.where("aid").is(aid)), Aggregation.unwind("$data"), Aggregation.project() .andExpression("year($data.datetime)") .as("year") .andExpression("month($data.datetime)") .as("month") .andExpression("dayOfMonth($data.datetime)") .as("day") .andInclude( "data", "aid", "mid", "author", "title", "focus", "forceFocus", "pic", "cCoin", "cDanmaku", "cShare", "cLike", "cFavorite", "tag", "cView", "cDatetime", "datetime", "channel", "danmaku_aggregate", "subChannel"), Aggregation.group( "year", "month", "day", "aid", "mid", "author", "title", "focus", "forceFocus", "pic", "cCoin", "cDanmaku", "danmaku_aggregate", "cShare", "cLike", "cFavorite", "cView", "cDatetime", "datetime", "channel", "tag", "subChannel") .first("data") .as("data"), Aggregation.group( "aid", "mid", "author", "title", "focus", "forceFocus", "pic", "cCoin", "cDanmaku", "cShare", "cLike", "cFavorite", "cView", "cDatetime", "datetime", "tag", "danmaku_aggregate", "channel", "subChannel") .push("data") .as("data")); return mongoTemplate.aggregate(a, "video", Video.class).getMappedResults().get(0); }
Example 13
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 14
Source File: VideoServiceImpl.java From biliob_backend with MIT License | 5 votes |
/** * get popular keyword * * @return keyword list */ @Override @Cacheable(value = "popular_keyword") public List getPopularTag() { int delta = 7; int compare = 90; VideoServiceImpl.logger.info("获取最流行的TAG列表"); Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, -delta); Aggregation a = Aggregation.newAggregation( Aggregation.match(Criteria.where("datetime").gt(c.getTime())), Aggregation.project("tag", "cView", "datetime"), Aggregation.unwind("tag"), Aggregation.group("tag").sum("cView").as("value").count().as("count"), Aggregation.match(Criteria.where("count").gt(10)), Aggregation.sort(Sort.Direction.DESC, "value"), Aggregation.limit(100)); c.add(Calendar.DATE, -compare); Date fe = c.getTime(); c.add(Calendar.DATE, -delta); Date fs = c.getTime(); Aggregation b = Aggregation.newAggregation( Aggregation.match(Criteria.where("datetime").lt(fe).gt(fs)), Aggregation.project("tag", "cView", "datetime"), Aggregation.unwind("tag"), Aggregation.group("tag").sum("cView").as("value").count().as("count"), Aggregation.match(Criteria.where("count").gt(10)), Aggregation.sort(Sort.Direction.DESC, "value"), Aggregation.limit(100)); Map temp = mongoTemplate.aggregate(b, "video", Map.class).getMappedResults().stream().reduce((res, item) -> { res.put(item.get("_id"), item.get("value")); return res; }).get(); List<Map> result = mongoTemplate.aggregate(a, "video", Map.class).getMappedResults(); result.forEach(e -> { if (temp.get(e.get("_id")) != null) { e.put("value", (Integer) e.get("value") - (Integer) temp.get(e.get("_id"))); } }); return result; }
Example 15
Source File: TracerServiceImpl.java From biliob_backend with MIT License | 5 votes |
private void getBucketUserCreditList(Map<String, Object> resultMap) { Aggregation bucketUserCreditAggregation = Aggregation.newAggregation(Aggregation.bucketAuto("exp", 20)); AggregationResults<Map> bucketUserCreditAggregationResult = mongoTemplate.aggregate(bucketUserCreditAggregation, "user", Map.class); ArrayList bucketUserCreditList = (ArrayList) bucketUserCreditAggregationResult.getRawResults().get("results"); resultMap.put("userBucketResult", bucketUserCreditList); }
Example 16
Source File: AuthorGroupServiceImpl.java From biliob_backend with MIT License | 5 votes |
@Override public List<GroupUpdateRecord> listChangeLog(String gid) { Aggregation a = Aggregation.newAggregation( Aggregation.match(Criteria.where("gid").is(new ObjectId(gid))), Aggregation.lookup("user", "userId", "_id", "user"), Aggregation.unwind("user"), Aggregation.project("message", "date").andExpression("{'nickName': 1}").as("user") ); return mongoTemplate.aggregate(a, GroupUpdateRecord.class, GroupUpdateRecord.class).getMappedResults(); }
Example 17
Source File: AuthorServiceImpl.java From biliob_backend with MIT License | 5 votes |
/** * list author tag * * @param mid author id * @return tag list */ @Override public List<Map> listAuthorTag(Long mid, Integer limit) { Aggregation a = Aggregation.newAggregation(Aggregation.match(Criteria.where("mid").is(mid)), Aggregation.unwind("tag"), Aggregation.project("tag", "cView"), Aggregation.group("tag").sum("cView").as("totalView").count().as("count"), Aggregation.sort(Sort.Direction.DESC, "count"), Aggregation.limit(limit)); return mongoTemplate.aggregate(a, "video", Map.class).getMappedResults(); }
Example 18
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 19
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 20
Source File: AuthorServiceImpl.java From biliob_backend with MIT License | 4 votes |
private Author getAggregatedData(Long mid, int days) { Calendar timer = Calendar.getInstance(); MatchOperation match = getAggregateMatch(days, mid); Aggregation a = Aggregation.newAggregation( match, Aggregation.project("fans", "archiveView", "articleView", "like", "attention", "datetime", "mid").and("datetime").dateAsFormattedString("%Y-%m-%d").as("date"), Aggregation.group("date") .first("datetime").as("datetime") .first("fans").as("fans") .first("archiveView").as("archiveView") .first("articleView").as("articleView") .first("like").as("like") .first("attention").as("attention") .first("mid").as("mid"), Aggregation.sort(Sort.Direction.DESC, "datetime"), Aggregation.group().push( new BasicDBObject("datetime", "$datetime") .append("fans", "$fans") .append("archiveView", "$archiveView") .append("articleView", "$articleView") .append("archive", "$archive") .append("article", "$article") .append("like", "$like") ).as("data").first("mid").as("mid"), Aggregation.lookup("author", "mid", "mid", "author"), Aggregation.unwind("author"), (aoc) -> new Document("$addFields", new Document("author.data", "$data")), Aggregation.replaceRoot("author"), Aggregation.lookup("author_interval", "mid", "mid", "interval"), (aoc) -> new Document("$addFields", new Document("obInterval", new Document("$arrayElemAt", Arrays.asList("$interval.interval", 0)))), Aggregation.lookup("author_achievement", "mid", "author.mid", "achievements"), Aggregation.project().andExpression("{ mid: 0}").as("data") ); Author data = mongoTemplate.aggregate(a, Author.Data.class, Author.class).getUniqueMappedResult(); long deltaTime = Calendar.getInstance().getTimeInMillis() - timer.getTimeInMillis(); // 太慢,则精简数据 logger.info(deltaTime); if (deltaTime > 7000) { adminService.reduceByMid(mid); } return data; }