你好,我在Collection上运行聚合操作时遇到此错误。我在FarmerCropDataLog集合中有1000个条目。
{
"_id" : ObjectId("5c7a590350d039fdc86e4e26"),
"cropData" : {
"cropName" : "PADDY",
"crop" : "PADDY",
"cropAcres" : 60.0,
"cropYield" : 21.0,
"cropPrice" : 6183.0
},
"creationTime" : "1551521509924",
"villageId" : "581edb47e4b0b5b1ebbfe753"
}
我想获取按村庄分组的cropPrice的平均价值。所以我写了下面的代码。
public void getComparisonSheet(GetComparisonSheetRequest getComparisonSheet, BasicResponse response,
String requestFarmerId) {
Farmer farmer = this.getFarmerById(requestFarmerId);
// get logs before this year..
String villageId = farmer.getVillageId();
MatchOperation matchOperation = Aggregation.match(Criteria.where(FarmerCropDataLog.Constants.CROP_LOG)
.elemMatch(Criteria.where(CropData.Constants.CROP).is(getComparisonSheet.getCrop())
.and(FarmerCropDataLog.Constants.VILLAGE_ID).is(villageId)
.and(FarmerCropDataLog.Constants.CREATION_TIME).gt(LAST_YEAR)));
GroupOperation groupOperation = Aggregation.group(FarmerCropDataLog.Constants.VILLAGE_ID);
groupOperation.avg(CropData.Constants.CROP_PRICE).as(GetComparisonSheetResponse.Constants.AVERAGE);
groupOperation.max(CropData.Constants.CROP_PRICE).as(GetComparisonSheetResponse.Constants.MAX_INCOME);
LimitOperation limitOperation = new LimitOperation(1);
Fields fields = Fields.fields(GetComparisonSheetResponse.Constants.AVERAGE,GetComparisonSheetResponse.Constants.MAX_INCOME);
ProjectionOperation projectionOperation = Aggregation.project(fields);
Aggregation aggregation = Aggregation.newAggregation(matchOperation, groupOperation, projectionOperation,limitOperation);
AggregationResults<GetComparisonSheetResponse> aggregationResults = farmerCropDataLogDAO.runAggregation(aggregation, FarmerCropDataLog.class, GetComparisonSheetResponse.class);
List<GetComparisonSheetResponse> comparisonSheetResponses = aggregationResults.getMappedResults();
Float AverageIncome = comparisonSheetResponses.get(0).getAverageIncome();
Float MaxIncome = comparisonSheetResponses.get(0).getMaxIncome();
GetComparisonSheetResponse comparisonSheetResponse = new GetComparisonSheetResponse();
comparisonSheetResponse.setAverageIncome(AverageIncome);
comparisonSheetResponse.setMaxIncome(MaxIncome);
response.setResponse(comparisonSheetResponse);
response.setSuccess(true);
}
这是我遇到的错误:无效的参考'averageIncome'!
这是我的FarmerCropDataLog类的样子。
public class FarmerCropDataLog extends AbstractEntity {
private String farmerId;
private CropData cropData;
private String villageId;
}
这是我的CropData类。
public class CropData {
private String cropName;
private Crop crop;
private Float cropAcres;
private Float cropYield;
private Float cropPrice;
}