This is my JSON.
{
"_id" : ObjectId("5c7a590350d039fdc86e4e37"),
"cropData" : {
"cropName" : "RICE",
"crop" : "RICE",
"cropAcres" : 10.0,
"cropYield" : 73.0,
"cropPrice" : 3619.0
},
"creationTime" : "1551521509975",
"villageId" : "581edb59e4b0b5b1ebbfe757"
}
There are 1000 entries like this in Collection called FarmerCropDataLog
. I want to get highest and average values from this FarmerCropDataLog
for particular village. for this I wrote following code.
public void getSheet(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()))
.andOperator(Criteria.where(FarmerCropDataLog.Constants.VILLAGE_ID).is(villageId))
.andOperator(Criteria.where(FarmerCropDataLog.Constants.CREATION_TIME).gt(LAST_YEAR)));
GroupOperation groupOperation = Aggregation.group(FarmerCropDataLog.Constants.VILLAGE_ID);
groupOperation.avg(CropData.Constants.CROP_PRICE).as("averageIncome");
groupOperation.max(CropData.Constants.CROP_PRICE).as("maxIncome");
Aggregation aggregation = Aggregation.newAggregation(matchOperation,groupOperation);
AggregationResults<GetComparisonSheetResponse> aggregationResults = farmerCropDataLogDAO.runAggregation(aggregation,FarmerCropDataLog.class,GetComparisonSheetResponse.class);
List<GetComparisonSheetResponse> getResult = aggregationResults.getMappedResults();
GetComparisonSheetResponse comparisonSheetResponse = getResult.get(0);
response.setResponse(comparisonSheetResponse);
response.setSuccess(true);
}
I am getting error, when I try to run farmerCropDataLogDAO.runAggregation
. it throws exception, how can I run this successfully?
Exception Thrown :
Due to limitations of the com.mongodb.BasicDBObject, you can't add a second '$and' expression specified as '$and : [ { "creationTime" : { "$gt" : 1471228928}}]'. Criteria already contains '$and : [ { "villageId" : "5b0ed0bc77c8ae9704f65c43"}]'.