Spring Data MongoDB –聚合分组示例

在本教程中,我们将向您展示如何使用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(
    {
	$match : {_id:{$lt:10}}
    },
    { 
	$group : {_id : "$hosting", total : { $sum : 1 }}
    },
    {
	$sort : {total : -1}
    }
);

输出量

{ "_id" : "aws.amazon.com", "total" : 4 }
{ "_id" : "hostgator.com", "total" : 3 }
{ "_id" : "cloud.google.com", "total" : 2 }
{ "_id" : "godaddy.com", "total" : 1 }

2. Spring数据+ MongoDB聚合示例

这是Spring Data MongoDB中的等效示例。

DomainDaoImpl.java
package 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;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import com.mkyong.core.domain.model.Domain;
import com.mkyong.core.hosting.model.HostingCount;
import java.util.List;

@Repository
public class DomainDaoImpl implements DomainDao {

	@Autowired
	MongoTemplate mongoTemplate;
	
	public List<HostingCount> getHostingCount() {
	
		Aggregation agg = newAggregation(
			match(Criteria.where("_id").lt(10)),
			group("hosting").count().as("total"),
			project("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
package com.mkyong.core.hosting.model;

public class HostingCount {

	private String hosting;

	private long total;

	//...
}

更多例子
参阅此官方Spring数据–聚合框架支持

参考文献

  1. MongoDB –聚合概念
  2. MongoDB –聚合和组示例
  3. Spring.io –聚合框架支持

翻译自: https://mkyong.com/mongodb/spring-data-mongodb-aggregation-grouping-example/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值