2016-09-30 13 views
0

私はバネ集計を使用してダミーです。
私はこの実体文書持っています:SpringデータMongoDB集約 - 結果の量を取得

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

    private String projectId; 
    private String originalFileName; 

を、私は同じ名前でoriginalFileName(そうDocumentFile年代別にグループ化され、同じPROJECTIDは一度だけカウントする必要があります持っているdocumentFile Sの量を取得します)

これは私のアプローチですが、結果や量を取得する方法はわかりません。

+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()); 
} 
関連する問題