gpt4 book ai didi

MongoDb 聚合查询转换为 Spring-data

转载 作者:可可西里 更新时间:2023-11-01 09:50:39 26 4
gpt4 key购买 nike

我有一个聚合函数,其中该方法的输出结构是:

 {
"_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"}}}])

写在 spring 中的聚合是:

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);

输出是:聚合里程输出

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)

请为此提供帮助。

最佳答案

错误告诉您管道中的变量名称之一与您的 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);

关于MongoDb 聚合查询转换为 Spring-data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34826924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com