利用group操作,保证数据唯一,达到去重效果
// 查询条件
MatchOperation operation0 = Aggregation.match( Criteria.where("sensorcode").in(sensorcodes).andOperator(
Criteria.where("thattime").lte(d2),
Criteria.where("thattime").gte(d1)));
// 聚合查询,保证数据唯一性
GroupOperation operation2 = Aggregation.group("sensorcode","thattime", "senval", "sensortype", "confirm", "confirmman", "val_aver",
"val_min" , "val_max", "val_sum", "val_last", "sensor_ch", "stationcode");
SortOperation sortOperation = Aggregation.sort( Direction.ASC, "thattime");
// 声明给以下字段赋值
ProjectionOperation projectionOperation = Aggregation.project().andInclude("sensorcode")
.andInclude("thattime").andInclude("senval").andInclude("sensortype")
.andInclude("confirm").andInclude("confirmman").andInclude("val_aver")
.andInclude("val_min").andInclude("val_max").andInclude("val_sum")
.andInclude("val_last").andInclude("sensor_ch").andInclude("stationcode");
TypedAggregation<sensordata> noRepeatAggregation =
Aggregation.newAggregation(sensordata.class, operation0, operation2, projectionOperation, sortOperation);
AggregationResults<sensordata> noRepeatDataInfoVos = mongoTemplate.aggregate(noRepeatAggregation, collectionName,sensordata.class);
List<sensordata> noRepeatDataList = noRepeatDataInfoVos.getMappedResults();
System.out.println(JSON.toJSONString(noRepeatDataList));