我有一个聚合函数,其中方法输出的结构是:
{
"_id" : {
"vehicleNumber" : "HR55W8395",
"vehicleType" : "TYPE_32"
},
"mileage" : [
3.4200838876537736,
3.6082731400212595,
3.7118590539249254,
2.9805899622661784,
5.227747018794297,
3.222515049264743,
3.8845896154778603,
3.548054585322907,
3.010341324091653
]
}
聚合函数是
db.hop.aggregate([{$group : {_id : {vehicleNumber : "$vehicleNumber", vehicleType : "$vehicleType"}, mileage: {$push : "$mileage"}}}])
并且写在春天的聚合是:
AggregationOperation match = match(Criteria.where("startHubOutTime").gte(startDateTime).and("endHubInTime").lte(endDateTime).and("vehicleNumber").exists(true).and("mileage").exists(true));
//GroupOperationBuilder group = Aggregation.group("vehicleNumber","vehicleType").push("mileage");
AggregationOperation grAggregationOperation =Aggregation.group("vehicleNumber","vehicleType").push(new BasicDBObject("mileage","$mileage")).as("mileage"); ;//group.as("_id");
AggregationOperation project = Aggregation.project("mileage").andInclude("vehicleNumber","vehicleType");
Aggregation newAggregation = Aggregation.newAggregation(match, grAggregationOperation);
AggregationResults<AggregatedMileageOutput> aggregatedMileageOutputs = mongoTemplate.aggregate(newAggregation, Hop.class, AggregatedMileageOutput.class);
,输出为: AggregatedMileageOutput
class AggregatedMileageOutput {
private String vehicleNumber;
private VehicleType vehicleType;
private ArrayList<Double> mileage;
}
但在聚合操作期间我收到错误:
Request processing failed; nested exception is org.springframework.data.mapping.model.MappingException: No mapping metadata found for java.lang.Double
Stack trace :
org.springframework.data.mapping.model.MappingException: No mapping metadata found for java.lang.Double
org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:228)
org.springframework.data.mongodb.core.convert.MappingMongoConverter.readCollectionOrArray(MappingMongoConverter.java:906)
org.springframework.data.mongodb.core.convert.MappingMongoConverter.readValue(MappingMongoConverter.java:1183)
org.springframework.data.mongodb.core.convert.MappingMongoConverter.access$200(MappingMongoConverter.java:78)
org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1133)
org.springframework.data.mongodb.core.convert.MappingMongoConverter.getValueInternal(MappingMongoConverter.java:869)
org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:282)
org.springframework.data.mongodb.core.convert.MappingMongoConverter$1.doWithPersistentProperty(MappingMongoConverter.java:270)
org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:309)
org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:270)
org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:231)
org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:191)
org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:187)
org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:78)
org.springframework.data.mongodb.core.MongoTemplate$ReadDbObjectCallback.doWith(MongoTemplate.java:2215)
org.springframework.data.mongodb.core.MongoTemplate$UnwrapAndReadDbObjectCallback.doWith(MongoTemplate.java:2248)
org.springframework.data.mongodb.core.MongoTemplate.returnPotentiallyMappedResults(MongoTemplate.java:1533)
org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1509)
org.springframework.data.mongodb.core.MongoTemplate.aggregate(MongoTemplate.java:1435)
请为此提供帮助。
答案 0 :(得分:1)
错误告诉您管道中的一个变量名称与您的mongodb集合中的字段名称不同(拼写,大写/小写)。尝试重新编写聚合操作
Aggregation agg = newAggregation(
match(
Criteria.where("startHubOutTime").gte(startDateTime)
.and("endHubInTime").lte(endDateTime)
.and("vehicleNumber").exists(true)
.and("mileage").exists(true)
),
group("vehicleNumber", "vehicleType").push("mileage").as("mileage")
);
AggregationResults<AggregatedMileageOutput> results = mongoTemplate.aggregate(agg, Hop.class, AggregatedMileageOutput.class);
List<AggregatedMileageOutput> mappedResults = results.getMappedResults();
AggregatedMileageOutput firstItem = mappedResults.get(0);