废话不多说,直接上代码
public int syncTotal(Date startTime, Date endTime) {
int result = 0;
Criteria criteria = new Criteria().andOperator(Criteria.where("data_time").gte(startTime), Criteria.where("data_time").lte(endTime));
//创建aggreation
Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria),Aggregation.unwind("values"), Aggregation.group("mp_id").first("mp_id").as("mpId").count().as("total"), Aggregation.addFields().addFieldWithValue("type", "day_air").build());
AggregationResults<TempMpId> out = mongoTemplate.aggregate(aggregation, "day_air", TempMpId.class);
List<TempMpId> tempMpIdList = new ArrayList<>();
for (Iterator<TempMpId> iterator = out.iterator(); iterator.hasNext(); ) {
tempMpIdList.add(iterator.next());
}
return result;
}
1.使用criteria 创建过滤筛选条件
2.使用Aggregation.unwind("values")方法,拆分values子集合,此方法会把
{
"_id": "2204000000429520201204000000000",
"mp_id": "22040000004295",
"create_time": ISODate("2020-12-04T16:10:03.627Z"),
"data_day": NumberInt("20201204"),
"data_time": ISODate("2021-03-11T01:29:55.095Z"),
"gd_id": "125F58D3-B632-43B0-B6D8-1EA90977F33C",
"mn": "20150724000002",
"protocol": "0",
"update_time": ISODate("2020-12-04T16:10:03.627Z"),
"values": [
{
"cou_value": null,
"flag": null,
"avg_value": null,
"mstatus": NumberInt("6"),
"pollutant_code": "001",
"min_value": null,
"avg_zs": null,
"estatus": NumberInt("0"),
"astatus": NumberInt("0"),
"outlet_standard": "20",
"max_zs": null,
"min_zs": null,
"max_value": null,
"sstatus": NumberInt("0")
},
{
"cou_value": null,
"flag": null,
"avg_value": null,
"mstatus": NumberInt("6"),
"pollutant_code": "002",
"min_value": null,
"avg_zs": null,
"estatus": NumberInt("0"),
"astatus": NumberInt("0"),
"outlet_standard": "80",
"max_zs": null,
"min_zs": null,
"max_value": null,
"sstatus": NumberInt("0")
},
{
"cou_value": null,
"flag": null,
"avg_value": null,
"mstatus": NumberInt("6"),
"pollutant_code": "003",
"min_value": null,
"avg_zs": null,
"estatus": NumberInt("0"),
"astatus": NumberInt("0"),
"outlet_standard": "250",
"max_zs": null,
"min_zs": null,
"max_value": null,
"sstatus": NumberInt("0")
},
{
"cou_value": null,
"flag": null,
"avg_value": null,
"mstatus": NumberInt("6"),
"pollutant_code": "004",
"min_value": null,
"avg_zs": null,
"estatus": NumberInt("0"),
"astatus": NumberInt("0"),
"outlet_standard": "80",
"max_zs": null,
"min_zs": null,
"max_value": null,
"sstatus": NumberInt("0")
},
{
"cou_value": null,
"flag": null,
"avg_value": null,
"mstatus": NumberInt("6"),
"pollutant_code": "008",
"min_value": null,
"avg_zs": null,
"estatus": NumberInt("0"),
"astatus": NumberInt("0"),
"outlet_standard": "50",
"max_zs": null,
"min_zs": null,
"max_value": null,
"sstatus": NumberInt("0")
},
{
"pollutant_code": "B02",
"cou_value": null,
"min_value": null,
"flag": null,
"avg_value": null,
"outlet_standard": null,
"max_value": null
}
]
}
拆分成,这样如果进行count的话,会把子集的总数算进去
// 1
{
"_id": "2204000000429520201204000000000",
"mp_id": "22040000004295",
"create_time": ISODate("2020-12-04T16:10:03.627Z"),
"data_day": NumberInt("20201204"),
"data_time": ISODate("2021-03-11T01:29:55.095Z"),
"gd_id": "125F58D3-B632-43B0-B6D8-1EA90977F33C",
"mn": "20150724000002",
"protocol": "0",
"update_time": ISODate("2020-12-04T16:10:03.627Z"),
"values": {
"cou_value": null,
"flag": null,
"avg_value": null,
"mstatus": NumberInt("6"),
"pollutant_code": "001",
"min_value": null,
"avg_zs": null,
"estatus": NumberInt("0"),
"astatus": NumberInt("0"),
"outlet_standard": "20",
"max_zs": null,
"min_zs": null,
"max_value": null,
"sstatus": NumberInt("0")
}
}
// 2
{
"_id": "2204000000429520201204000000000",
"mp_id": "22040000004295",
"create_time": ISODate("2020-12-04T16:10:03.627Z"),
"data_day": NumberInt("20201204"),
"data_time": ISODate("2021-03-11T01:29:55.095Z"),
"gd_id": "125F58D3-B632-43B0-B6D8-1EA90977F33C",
"mn": "20150724000002",
"protocol": "0",
"update_time": ISODate("2020-12-04T16:10:03.627Z"),
"values": {
"cou_value": null,
"flag": null,
"avg_value": null,
"mstatus": NumberInt("6"),
"pollutant_code": "002",
"min_value": null,
"avg_zs": null,
"estatus": NumberInt("0"),
"astatus": NumberInt("0"),
"outlet_standard": "80",
"max_zs": null,
"min_zs": null,
"max_value": null,
"sstatus": NumberInt("0")
}
}
// 3
{
"_id": "2204000000429520201204000000000",
"mp_id": "22040000004295",
"create_time": ISODate("2020-12-04T16:10:03.627Z"),
"data_day": NumberInt("20201204"),
"data_time": ISODate("2021-03-11T01:29:55.095Z"),
"gd_id": "125F58D3-B632-43B0-B6D8-1EA90977F33C",
"mn": "20150724000002",
"protocol": "0",
"update_time": ISODate("2020-12-04T16:10:03.627Z"),
"values": {
"cou_value": null,
"flag": null,
"avg_value": null,
"mstatus": NumberInt("6"),
"pollutant_code": "003",
"min_value": null,
"avg_zs": null,
"estatus": NumberInt("0"),
"astatus": NumberInt("0"),
"outlet_standard": "250",
"max_zs": null,
"min_zs": null,
"max_value": null,
"sstatus": NumberInt("0")
}
}
// 4
{
"_id": "2204000000429520201204000000000",
"mp_id": "22040000004295",
"create_time": ISODate("2020-12-04T16:10:03.627Z"),
"data_day": NumberInt("20201204"),
"data_time": ISODate("2021-03-11T01:29:55.095Z"),
"gd_id": "125F58D3-B632-43B0-B6D8-1EA90977F33C",
"mn": "20150724000002",
"protocol": "0",
"update_time": ISODate("2020-12-04T16:10:03.627Z"),
"values": {
"cou_value": null,
"flag": null,
"avg_value": null,
"mstatus": NumberInt("6"),
"pollutant_code": "004",
"min_value": null,
"avg_zs": null,
"estatus": NumberInt("0"),
"astatus": NumberInt("0"),
"outlet_standard": "80",
"max_zs": null,
"min_zs": null,
"max_value": null,
"sstatus": NumberInt("0")
}
}
// 5
{
"_id": "2204000000429520201204000000000",
"mp_id": "22040000004295",
"create_time": ISODate("2020-12-04T16:10:03.627Z"),
"data_day": NumberInt("20201204"),
"data_time": ISODate("2021-03-11T01:29:55.095Z"),
"gd_id": "125F58D3-B632-43B0-B6D8-1EA90977F33C",
"mn": "20150724000002",
"protocol": "0",
"update_time": ISODate("2020-12-04T16:10:03.627Z"),
"values": {
"cou_value": null,
"flag": null,
"avg_value": null,
"mstatus": NumberInt("6"),
"pollutant_code": "008",
"min_value": null,
"avg_zs": null,
"estatus": NumberInt("0"),
"astatus": NumberInt("0"),
"outlet_standard": "50",
"max_zs": null,
"min_zs": null,
"max_value": null,
"sstatus": NumberInt("0")
}
}
// 6
{
"_id": "2204000000429520201204000000000",
"mp_id": "22040000004295",
"create_time": ISODate("2020-12-04T16:10:03.627Z"),
"data_day": NumberInt("20201204"),
"data_time": ISODate("2021-03-11T01:29:55.095Z"),
"gd_id": "125F58D3-B632-43B0-B6D8-1EA90977F33C",
"mn": "20150724000002",
"protocol": "0",
"update_time": ISODate("2020-12-04T16:10:03.627Z"),
"values": {
"pollutant_code": "B02",
"cou_value": null,
"min_value": null,
"flag": null,
"avg_value": null,
"outlet_standard": null,
"max_value": null
}
}
3.Aggregation.group("mp_id").first("mp_id").as("mpId").count().as("total")进行group,并且第一列mp_id起别名为mpId,count计数列起别名为total
4.Aggregation.addFields().addFieldWithValue("type", "day_air").build()) 添加自定义字段
5.AggregationResults<TempMpId> out = mongoTemplate.aggregate(aggregation, "day_air", TempMpId.class); 参数1为聚合配置,参数2为查询那个文档,参数3为接收的实体类
6.List<TempMpId> tempMpIdList = new ArrayList<>(); for (Iterator<TempMpId> iterator = out.iterator(); iterator.hasNext(); ) { tempMpIdList.add(iterator.next()); } 循环取出返回值
7.接收返回值的实体类说明
public class TempMpId implements Serializable {
private String mpId;
private Integer total;
private String type;
public String getMpId() {
return mpId;
}
public void setMpId(String mpId) {
this.mpId = mpId;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}