org.springframework.data.mongodb.core.aggregation.Aggregation Java Examples

The following examples show how to use org.springframework.data.mongodb.core.aggregation.Aggregation. 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 vote down vote up
@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 vote down vote up
/**
 * 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: ReviewRepositoryImpl.java    From mirrorgate with Apache License 2.0 8 votes vote down vote up
@Override
public List<ApplicationDTO> getAverageRateByAppNamesAfterTimestamp(final List<String> names, final Long timestamp) {

    final Aggregation aggregation = newAggregation(
        match(Criteria.where("appname").in(names).and("timestamp").gte(timestamp)),
        group("appname", "platform")
            .first("appname").as("appname")
            .first("platform").as("platform")
            .count().as("votesShortTerm")
            .sum("starrating").as("ratingShortTerm")
    );

    //Convert the aggregation result into a List
    final AggregationResults<ApplicationDTO> groupResults
        = mongoTemplate.aggregate(aggregation, Review.class, ApplicationDTO.class);

    return groupResults.getMappedResults();
}
 
Example #4
Source File: IssueRepositoryImpl.java    From mirrorgate with Apache License 2.0 7 votes vote down vote up
@Override
public double getBacklogEstimateByKeywords(final List<String> boards) {

    final Aggregation agg = newAggregation(
        match(Criteria
            .where("keywords").in(boards)
            .and("sprintAssetState").ne("ACTIVE")
            .and("type").in(Arrays.asList(IssueType.STORY.getName(), IssueType.BUG.getName()))
            .and("status").nin(Arrays.asList(IssueStatus.ACCEPTED.getName(), IssueStatus.DONE.getName()))
        ),
        group()
            .sum("estimation")
            .as("value"),
        project("value")
            .andExclude("_id")
    );

    final AggregationResults<DoubleValue> groupResults
        = mongoTemplate.aggregate(agg, "issue", DoubleValue.class);
    final DoubleValue val = groupResults.getUniqueMappedResult();

    return val == null ? 0 : val.value;
}
 
Example #5
Source File: DependencySimilarityCalculator.java    From scava with Eclipse Public License 2.0 7 votes vote down vote up
@Override
public int getNumberOfProjectThatUseDependencies(String dependency) {

	Aggregation aggregation = newAggregation(
			unwind("dependencies"),
			group("dependencies").count().as("count"),
			project("count").and("dependencies").previousOperation(),
			sort(Direction.DESC,"count"),
			match(Criteria.where("dependencies").is(dependency))
		);
	AggregationResults<DependencyCount> results = mongoOperations.aggregate(aggregation, "artifact", DependencyCount.class);
	List<DependencyCount> result = results.getMappedResults();
	if(!result.isEmpty())	
		return result.get(0).getCount();
	else return 0;
}
 
Example #6
Source File: TracerServiceImpl.java    From biliob_backend with MIT License 7 votes vote down vote up
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 #7
Source File: DependencySimilarityCalculator.java    From scava with Eclipse Public License 2.0 7 votes vote down vote up
@Override
public Map<String, Integer> getMapDependencyAndNumOfProjectThatUseIt() {
	Map<String, Integer> result = new HashMap<>();
	Aggregation aggregation = newAggregation(
			unwind("dependencies"),
			group("dependencies").count().as("count"),
			project("count").and("dependencies").previousOperation(),
			sort(Direction.DESC,"count")
		);
	AggregationResults<DependencyCount> results = mongoOperations.aggregate(aggregation, "artifact", DependencyCount.class);
	List<DependencyCount> intermediateResult = results.getMappedResults();
	for (DependencyCount dependencyCount : intermediateResult) {
		result.put(dependencyCount.getDependencies(), dependencyCount.getCount());
	}
	return result;
}
 
Example #8
Source File: MySpringBootApplicationTests.java    From spring-boot-101 with Apache License 2.0 7 votes vote down vote up
@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 #9
Source File: VideoServiceImpl.java    From biliob_backend with MIT License 7 votes vote down vote up
/**
 * 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 #10
Source File: VideoServiceImpl.java    From biliob_backend with MIT License 7 votes vote down vote up
/**
 * 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 #11
Source File: ReviewRepositoryImpl.java    From mirrorgate with Apache License 2.0 7 votes vote down vote up
@Override
public List<ApplicationDTO> getAppInfoByAppNames(final List<String> names) {

    final Aggregation aggregation = newAggregation(
        match(Criteria.where("appname").in(names).and("timestamp").exists(true)),
        sort(Sort.by(DESC, "timestamp")),
        project("appname", "platform", "starrating",
            "timestamp", "comment", "authorName", "url"),
        group("appname", "platform")
            .push(new BasicDBObject("author", "$authorName")
                .append("rate", "$starrating")
                .append("timestamp", "$timestamp")
                .append("comment", "$comment")
                .append("url", "$url")
            ).as("reviews"),
        project("appname", "platform")
            .and("reviews").slice(8, 0)
    );

    //Convert the aggregation result into a List
    final AggregationResults<ApplicationDTO> groupResults
        = mongoTemplate.aggregate(aggregation, Review.class, ApplicationDTO.class);

    return groupResults.getMappedResults();
}
 
Example #12
Source File: SpringBooksIntegrationTests.java    From spring-data-examples with Apache License 2.0 7 votes vote down vote up
/**
 * Retrieve the number of pages per author (and divide the number of pages by the number of authors).
 */
@Test
public void shouldRetrievePagesPerAuthor() {

	Aggregation aggregation = newAggregation( //
			match(Criteria.where("volumeInfo.authors").exists(true)), //
			replaceRoot("volumeInfo"), //
			project("authors", "pageCount") //
					.and(ArithmeticOperators.valueOf("pageCount") //
							.divideBy(ArrayOperators.arrayOf("authors").length()))
					.as("pagesPerAuthor"),
			unwind("authors"), //
			group("authors") //
					.sum("pageCount").as("totalPageCount") //
					.sum("pagesPerAuthor").as("approxWritten"), //
			sort(Direction.DESC, "totalPageCount"));

	AggregationResults<PagesPerAuthor> result = operations.aggregate(aggregation, "books", PagesPerAuthor.class);

	PagesPerAuthor pagesPerAuthor = result.getMappedResults().get(0);

	assertThat(pagesPerAuthor.getAuthor()).isEqualTo("Josh Long");
	assertThat(pagesPerAuthor.getTotalPageCount()).isEqualTo(1892);
	assertThat(pagesPerAuthor.getApproxWritten()).isEqualTo(573);
}
 
Example #13
Source File: CommitRepositoryImpl.java    From mirrorgate with Apache License 2.0 7 votes vote down vote up
@Override
public Double getSecondsToMaster(final List<String> repositories, final long timestamp) {
    final Aggregation agg = newAggregation(
        match(getCriteriaExpressionsForRepositories(repositories)
            .and("timestamp").gte(timestamp)
        ),
        group()
            .avg(
                Ceil.ceilValueOf(
                    Subtract.valueOf("$branches.refs/remotes/origin/master").subtract("timestamp")
                )
            ).as("value"),
        project("value")
            .andExclude("_id")
    );

    final AggregationResults<DoubleValue> secondsToMaster = mongoTemplate
        .aggregate(agg, "commits", DoubleValue.class);
    final DoubleValue seconds = secondsToMaster.getUniqueMappedResult();

    return seconds != null ? seconds.value : null;
}
 
Example #14
Source File: IssueRepositoryImpl.java    From mirrorgate with Apache License 2.0 7 votes vote down vote up
@Override
public List<String> programIncrementBoardFeatures(
    final List<String> boards,
    final List<String> programIncrementFeatures
) {

    final Aggregation agg = newAggregation(
        match(Criteria
            .where("parentsKeys").in(programIncrementFeatures)
            .and("keywords").in(boards)
        ),
        unwind("parentsKeys"),
        group()
            .addToSet("parentsKeys")
            .as("features"),
        project("features")
            .andExclude("_id")
    );

    final AggregationResults<ProgramIncrementBoardFeatures> aggregationResult
        = mongoTemplate.aggregate(agg, "issue", ProgramIncrementBoardFeatures.class);

    return aggregationResult.getUniqueMappedResult() != null ? aggregationResult.getUniqueMappedResult().features
        : new ArrayList<>();
}
 
Example #15
Source File: TxManagerInfoServiceImpl.java    From Lottor with MIT License 7 votes vote down vote up
/**
 * 按照日期获取tx的数量
 *
 * @return
 */
@Override
public Map<String, String> clientDetails(Boolean source) {
    Map<String, String> res = new HashMap<>();
    String attr = source ? "source" : "target";
    Arrays.stream(ServiceNameEnum.values()).forEach(serviceNameEnum -> {
        String service = serviceNameEnum.getServiceName();
        Aggregation aggregation = Aggregation.newAggregation(
                match(new Criteria(attr).is(service)),
                group(attr).count().as("count")
        );
        AggregationResults<BasicDBObject> results = mongoTemplate.aggregate(aggregation,
                CollectionNameEnum.TransactionMsg.name(), BasicDBObject.class);
        List<BasicDBObject> dbObjects = results.getMappedResults();
        for (int i = 0; i < dbObjects.size(); i++) {
            BasicDBObject data = dbObjects.get(i);
            res.put(data.getString("_id"), data.getString("count"));
        }
    });

    return res;
}
 
Example #16
Source File: ArticleCommentRepositoryImpl.java    From jakduk-api with MIT License 7 votes vote down vote up
/**
 * 게시물 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 #17
Source File: VideoServiceV2.java    From biliob_backend with MIT License 7 votes vote down vote up
private Video getVideoWithAuthorDataByCriteria(Criteria c) {

        Video v = mongoTemplate.aggregate(Aggregation.newAggregation(
                Aggregation.match(c),
                Aggregation.lookup("author", "mid", "mid", "authorList"),
                Aggregation.project().andExpression("{data:0 , keyword:0, fansRate: 0, follows: 0, rank: 0}").as("authorList")
        ), Video.class, Video.class).getUniqueMappedResult();

        if (v == null) {
            return null;
        }
        if (v.getAuthorList() != null && v.getAuthorList().size() == 1) {
            v.setAuthor(v.getAuthorList().get(0));
        }
        v.setAuthorList(null);
        v.setKeyword(null);
        return v;
    }
 
Example #18
Source File: IssueRepositoryImpl.java    From mirrorgate with Apache License 2.0 7 votes vote down vote up
@Override
public ProgramIncrementNamesAggregationResult getProductIncrementFromPiPattern(final Pattern pi) {

    final Aggregation agg = newAggregation(
        match(Criteria
            .where("type").is(IssueType.FEATURE.getName())
        ),
        project("piNames").andExclude("_id"),
        unwind("piNames"),
        match(Criteria
            .where("piNames").is(pi)
        ),
        group().addToSet("piNames").as("piNames")
    );

    final AggregationResults<ProgramIncrementNamesAggregationResult> aggregationResult
        = mongoTemplate.aggregate(agg, "issue", ProgramIncrementNamesAggregationResult.class);

    return aggregationResult.getUniqueMappedResult();
}
 
Example #19
Source File: SiteServiceImpl.java    From biliob_backend with MIT License 7 votes vote down vote up
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 #20
Source File: JakdukDAO.java    From jakduk-api with MIT License 7 votes vote down vote up
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 #21
Source File: ReviewRepositoryImpl.java    From mirrorgate with Apache License 2.0 6 votes vote down vote up
@Override
public List<ApplicationReviewsDTO> getLastReviewPerApplication(final List<String> names) {

    final Aggregation aggregation = newAggregation(
        match(Criteria.where("appname").in(names)),
        sort(Sort.by(DESC, "timestamp")),
        group("appname", "platform")
            .first("platform").as("platform")
            .first("appname").as("appName")
            .first("appname").as("appId")
            .first("commentId").as("commentId")
    );

    //Convert the aggregation result into a List
    final AggregationResults<ApplicationReviewsDTO> groupResults
        = mongoTemplate.aggregate(aggregation, Review.class, ApplicationReviewsDTO.class);

    return groupResults.getMappedResults();
}
 
Example #22
Source File: TracerServiceImpl.java    From biliob_backend with MIT License 6 votes vote down vote up
private void getMonthlySignIn(Map<String, Object> resultMap) {
    resultMap.put(
            "monthlySignIn",
            mongoTemplate
                    .aggregate(
                            Aggregation.newAggregation(
                                    Aggregation.project()
                                            .andExpression("month(_id)")
                                            .as("month")
                                            .andExpression("year(_id)")
                                            .as("year"),
                                    Aggregation.group("year", "month").count().as("count"),
                                    Aggregation.sort(Sort.by("year").ascending().and(Sort.by("month").ascending())),
                                    Aggregation.skip(1L),
                                    Aggregation.limit(10)),
                            "user",
                            Map.class)
                    .getMappedResults());
}
 
Example #23
Source File: CommitRepositoryImpl.java    From mirrorgate with Apache License 2.0 6 votes vote down vote up
@Override
public Double getCommitsPerDay(final List<String> repositories, final long timestamp, final int daysBefore) {

    final Aggregation agg = newAggregation(
        match(getCriteriaExpressionsForRepositories(repositories)
            .and("timestamp").gte(timestamp)
        ),
        group()
            .count().as("value"),
        project("value")
            .andExclude("_id").and("value").divide(daysBefore)
    );

    final AggregationResults<DoubleValue> commitsPerDay
        = mongoTemplate.aggregate(agg, "commits", DoubleValue.class);
    final DoubleValue commits = commitsPerDay.getUniqueMappedResult();

    return commits != null ? commits.value : null;
}
 
Example #24
Source File: TracerServiceImpl.java    From biliob_backend with MIT License 6 votes vote down vote up
private void getWeeklyCheckIn(Map<String, Object> resultMap) {
    resultMap.put(
            "weeklyCheckIn",
            mongoTemplate
                    .aggregate(
                            Aggregation.newAggregation(
                                    Aggregation.match(Criteria.where("message").is("签到")),
                                    Aggregation.project()
                                            .andExpression("week(_id)")
                                            .as("week")
                                            .andExpression("year(_id)")
                                            .as("year"),
                                    Aggregation.group("year", "week").count().as("count"),
                                    Aggregation.sort(Sort.by("year").ascending().and(Sort.by("week").ascending())),
                                    Aggregation.skip(1L),
                                    Aggregation.limit(10)),
                            "user_record",
                            Map.class)
                    .getMappedResults());
}
 
Example #25
Source File: SiteServiceImpl.java    From biliob_backend with MIT License 6 votes vote down vote up
@Override
public List<?> groupOnline(Integer days) {
    days = days > 365 ? 365 : days;
    Calendar c = Calendar.getInstance();
    c.add(Calendar.DATE, -days);
    return mongoTemplate.aggregate(
            Aggregation.newAggregation(
                    Aggregation.match(Criteria.where("datetime").gt(c.getTime())),
                    Aggregation.project()
                            .and("datetime").dateAsFormattedString("%Y-%m-%d").as("date")
                            .and("play_online").as("value")
                            .andExpression("week($datetime)").as("week"),
                    Aggregation.group("date")
                            .max("value").as("value").first("week").as("week"),
                    Aggregation.project("value", "week").and("_id").as("date"),
                    Aggregation.sort(Sort.Direction.ASC, "date"),
                    Aggregation.group("week")
                            .last("date").as("date")
                            .max("value").as("max")
                            .min("value").as("min")
                            .first("value").as("first")
                            .last("value").as("last")
            ), "site_info", StockData.class
    ).getMappedResults();
}
 
Example #26
Source File: IndexServiceImpl.java    From biliob_backend with MIT License 6 votes vote down vote up
@Override
    public JannchieIndex getIndex(String keyword) {
        logger.info(keyword);
        Criteria criteria = Criteria.where("keyword").is(keyword);
        JannchieIndex jannchieIndex = new JannchieIndex();
        jannchieIndex.setName(keyword);
        List<JannchieIndexData> data = mongoTemplate.aggregate(
                Aggregation.newAggregation(
                        Aggregation.match(Criteria.where("cJannchie").gt(1000000)),
                        Aggregation.match(criteria),
                        Aggregation.unwind("data"),
                        Aggregation.project()
//                                .and("$data.jannchie").arrayElementAt(0).as("jannchie")
                                .and("title").as("title")
                                .and("$data.jannchie").as("jannchie")
                                .and("$data.datetime").dateAsFormattedString("%Y-%m").as("datetime"),
                        Aggregation.group("title", "datetime").first("jannchie").as("jannchie").first("datetime").as("datetime"),
                        Aggregation.group("datetime").sum("jannchie").as("jannchie"),
                        Aggregation.project("jannchie").and("_id").as("datetime"),
                        Aggregation.match(Criteria.where("jannchie").ne(0)),
                        Aggregation.sort(Sort.Direction.ASC, "datetime")
                )
                , Video.class, JannchieIndexData.class).getMappedResults();
        jannchieIndex.setData(data);
        return jannchieIndex;
    }
 
Example #27
Source File: SpringBooksIntegrationTests.java    From spring-data-examples with Apache License 2.0 6 votes vote down vote up
/**
 * Filter for Data-related books in their title and output the title and authors.
 */
@Test
public void shouldRetrieveDataRelatedBooks() {

	Aggregation aggregation = newAggregation( //
			match(Criteria.where("volumeInfo.title").regex("data", "i")), //
			replaceRoot("volumeInfo"), //
			project("title", "authors"), //
			sort(Direction.ASC, "title"));

	AggregationResults<BookAndAuthors> result = operations.aggregate(aggregation, "books", BookAndAuthors.class);

	BookAndAuthors bookAndAuthors = result.getMappedResults().get(1);

	assertThat(bookAndAuthors.getTitle()).isEqualTo("Spring Data");
	assertThat(bookAndAuthors.getAuthors()).contains("Mark Pollack", "Oliver Gierke", "Thomas Risberg", "Jon Brisbin",
			"Michael Hunger");
}
 
Example #28
Source File: ZipsAggregationLiveTest.java    From tutorials with MIT License 6 votes vote down vote up
@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 #29
Source File: JakdukDAO.java    From jakduk-api with MIT License 6 votes vote down vote up
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 #30
Source File: SpringBooksIntegrationTests.java    From spring-data-examples with Apache License 2.0 6 votes vote down vote up
/**
 * Get number of books that were published by the particular publisher.
 */
@Test
public void shouldRetrieveBooksPerPublisher() {

	Aggregation aggregation = newAggregation( //
			group("volumeInfo.publisher") //
					.count().as("count"), //
			sort(Direction.DESC, "count"), //
			project("count").and("_id").as("publisher"));

	AggregationResults<BooksPerPublisher> result = operations.aggregate(aggregation, "books", BooksPerPublisher.class);

	assertThat(result).hasSize(27);
	assertThat(result).extracting("publisher").containsSequence("Apress", "Packt Publishing Ltd");
	assertThat(result).extracting("count").containsSequence(26, 22, 11);
}