MongoDB-2-聚合去重查询,并返回所有字段值

利用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 noRepeatAggregation =
				Aggregation.newAggregation(sensordata.class, operation0,  operation2, projectionOperation, sortOperation);
AggregationResults noRepeatDataInfoVos = mongoTemplate.aggregate(noRepeatAggregation, collectionName,sensordata.class);
List noRepeatDataList = noRepeatDataInfoVos.getMappedResults();
System.out.println(JSON.toJSONString(noRepeatDataList));
		

你可能感兴趣的:(MongoDB,mongodb)