2016-09-30 109 views
0

我是使用彈簧聚合的虛擬人。
我確實有這個實體文件:Spring Data MongoDB聚合 - 獲取結果量

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

    private String projectId; 
    private String originalFileName; 

我一定要得到的具有相同專案編號分組由originalFileName(所以DocumentFile的同名應該只計算一次documentFile S上的量)

這是我的方法,但我不知道如何得到結果/數量。

final Aggregation agg = newAggregation(match(Criteria.where("projectId").in(projectId)), 
     group("originalFileName").count().as("amountOfDocumentFiles")); 
+0

你想各種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()); 
}