このチュートリアルでは、Spring Data MongoDB集約フレームワークでデータのグループ化を行う方法を説明します。
1.テストデータ
domain.json
{ "__id" : 1, "domainName" : "test1.com", "hosting" : "hostgator.com" }
{ "__id" : 2, "domainName" : "test2.com", "hosting" : "aws.amazon.com"}
{ "__id" : 3, "domainName" : "test3.com", "hosting" : "aws.amazon.com" }
{ "__id" : 4, "domainName" : "test4.com", "hosting" : "hostgator.com" }
{ "__id" : 5, "domainName" : "test5.com", "hosting" : "aws.amazon.com" }
{ "__id" : 6, "domainName" : "test6.com", "hosting" : "cloud.google.com" }
{ "__id" : 7, "domainName" : "test7.com", "hosting" : "aws.amazon.com" }
{ "__id" : 8, "domainName" : "test8.com", "hosting" : "hostgator.com" }
{ "__id" : 9, "domainName" : "test9.com", "hosting" : "cloud.google.com" }
{ "__id" : 10, "domainName" : "test10.com", "hosting" : "godaddy.com" }
...
{"__id":100、 "domainName": "test10.com"、 "hosting": "godaddy.com"}
2. MongoDB集約の例
MongoDB集約の例は、ホストの総数を合計する:
db.domain.aggregate(
{
$一致:{__id:{$ lt:10}}
}、
{
$ group:{__id: "$ hosting"、合計:{$ sum:1}}
}、
{
$ソート:{合計:-1}
}
);
出力
{"__id": "aws.amazon.com"、 "合計":4}
{"__id": "hostgator.com"、 "total":3}
{"__id": "cloud.google.com"、 "合計":2}
{"__id": "godaddy.com"、 "合計":1}
2. Springデータ+ MongoDB集約の例
これは、Spring Data MongoDBの同等の例です。
DomainDaoImpl.java
パッケージcom.mkyong.core.domain.dao;
//imports as static import static org.springframework.data.mongodb.core.aggregation.Aggregation.group; import static org.springframework.data.mongodb.core.aggregation.Aggregation.match; import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation; import static org.springframework.data.mongodb.core.aggregation.Aggregation.project; import static org.springframework.data.mongodb.core.aggregation.Aggregation.sort;
インポートorg.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.MongoTemplate;インポートorg.springframework.data.mongodb.core.aggregation.Aggregation; org.springframework.data.mongodb.core.aggregation.AggregationResultsをインポートします。インポートorg.springframework.data.mongodb.core.query.Criteria;インポートorg.springframework.data.mongodb.core.query.Query;インポートorg.springframework.data.mongodb.core.query.Update;インポートorg.springframework.stereotype.Repository; import com.mkyong.core.domain.model.Domain; import com.mkyong.core.hosting.model.HostingCount; import java.util.List;
@RepositoryパブリッククラスDomainDaoImplはDomainDao {
@Autowired MongoTemplate mongoTemplate; ( "total")、プロジェクト( "total")、グループ( "hosting")、グループ( " ( "total")and( "hosting")。previousOperation()、sort(Sort.Direction.DESC、 "total"));
//Convert the aggregation result into a List
AggregationResults<HostingCount> groupResults
= mongoTemplate.aggregate(agg, Domain.class, HostingCount.class);
List<HostingCount> result = groupResults.getMappedResults();
return result;
}
HostingCount.java
パッケージcom.mkyong.core.hosting.model;
パブリッククラスHostingCount {
プライベートストリングホスティング;
プライベートロングトータル。
//...
}
。
MongoDB – 集約
コンセプト
。リンク://mongodb/mongodb-aggregate-and-group-example/[MongoDB –
集計およびグループの例]。
Spring.io
– 集約フレームワークのサポート
aggregation
mongodb
spring-data