【问题标题】:MongoDB Spring Aggregation throws ExceptionMongoDB Spring 聚合抛出异常
【发布时间】:2019-07-24 06:11:18
【问题描述】:

这是我的 JSON。

{
    "_id" : ObjectId("5c7a590350d039fdc86e4e37"),
    "cropData" : {
        "cropName" : "RICE",
        "crop" : "RICE",
        "cropAcres" : 10.0,
        "cropYield" : 73.0,
        "cropPrice" : 3619.0
    },
    "creationTime" : "1551521509975",
    "villageId" : "581edb59e4b0b5b1ebbfe757"
}

在名为 FarmerCropDataLog 的集合中有 1000 个这样的条目。我想从这个FarmerCropDataLog 获得特定村庄的最高值和平均值。为此,我编写了以下代码。

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

当我尝试运行 farmerCropDataLogDAO.runAggregation 时出现错误。它抛出异常,我怎样才能成功运行它?

抛出异常:

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"}]'.

【问题讨论】:

  • 例外是?您应该提供返回的任何错误,因为它提供了人们的上下文。
  • 它抛出了只打印内部错误的异常。
  • 您被要求在您的问题中实际显示任何返回的错误。请这样做。
  • @NeilLunn,已添加。
  • 你去吧,“上下文”!现代 API 版本(与过去 4 年一样)使用 Document 而不是 BasicDBObject。这是哪个版本的spring mongo?您是否正在遵循多年前编写的旧教程?您是否尝试过更新项目中的 spring-mongodb 依赖项?

标签: spring mongodb


【解决方案1】:

好的,我已经按照@Neil 的建议更改了代码,从而改变了我的问题,感谢他。

我已经从这里更改了这段代码

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

到这里->

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 2019-09-19
    相关资源
    最近更新 更多