2016-09-30 2 views
0

저는 스프링 집계를 사용하지 않습니다.
나는이 엔티티 문서가 수행스프링 데이터 MongoDB aggregation - 결과의 양을 얻으십시오.

@Document(collection = "DocumentFile") 
public class DocumentFile { 

    private String projectId; 
    private String originalFileName; 

와 나는 같은 projectIdoriginalFileName에 의해 (같은 이름을 가진 DocumentFile 년대는 한 번만 계산해야하므로 그룹화가 documentFile의 양을 얻을 것이다)

이것은 내 접근 방식이지만 지금 결과/금액을 얻는 방법을 모르겠습니다.

+0

가 특정 projectId 다양한 originalFileName의 수를 원하십니까? 이 컬렉션의 샘플 문서를 추가 할 수 있습니까? – notionquest

답변

0

게시물에있는 집계가 정확하다고 가정합니다. 다음은 MongoOperations를 사용하여 집계를 실행하고 결과를 얻는 샘플 코드입니다.

내 프로젝트에서 나는 이와 같은 MongoOperations 객체를 얻는다.

public MongoOperations getMongoConnection() { 

     return (MongoOperations) new AnnotationConfigApplicationContext(SpringMongoConfig.class) 
       .getBean("mongoTemplate"); 
    } 

집계 실행하고 결과를 얻을 : -

Aggregation aggregate = newAggregation(match(Criteria.where("projectId").in(projectId)), 
     group("originalFileName").count().as("amountOfDocumentFiles")); 

AggregationResults<DocumentFile> documentFileAggregate = mongoOperations.aggregate(aggregate, 
       "DocumentFile", DocumentFile.class); 

if (documentFileAggregate != null) { 
    System.out.println("Output ====>" + documentFileAggregate.getRawResults().get("result")); 
    System.out.println("Output ====>" + documentFileAggregate.getRawResults().toMap()); 
} 
관련 문제