0
0条评论

MongoDB从3.4.10 升级到4.1.0后,聚合语句报错

nested exception is com.mongodb.MongoCommandException: Command failed with error 9: ‘The ‘cursor’ option is required, except for aggregate with the explain argument’ on server 10.81.31.39:27117. The full response is { “ok” : 0.0, “errmsg” : “The ‘cursor’ option is required, except for aggregate with the explain argument”, “code” : 9, “codeName” : “FailedToParse” }

Java语句

Criteria c = Criteria.where("taTimestamp").gte(taTimestampMin)
        .andOperator(Criteria.where("taTimestamp").lte(taTimestampMax));
//org.springframework.data.mongodb.core.aggregation.Aggregation,用于组合一个统计需要的元素
Aggregation aggregation = Aggregation.newAggregation(
        Aggregation.match(c),
        //查询条件
        Aggregation.group("bsMeasureControlPointId")//获取的时候key是“_id”

                .last("bsSubCompanyId").as("bsSubCompanyId")
                .last("bsPipeId").as("bsPipeId")
                .last("bsLocateMileage").as("bsLocateMileage")
                .last("locationCode").as("locationCode")
                .last("theName").as("theName")
                .last("deviceType").as("deviceType")
                .last("sysType").as("sysType")
                .last("soilResistivity").as("soilResistivity")

                .last("poff").as("poffLast")
                .max("poff").as("poffMax")
                .min("poff").as("poffMin")
                .avg("poff").as("poffAvg")
                .sum("poff").as("poffSum")

                .last("von_Revised").as("vonLast")
                .max("von_Revised").as("vonMax")
                .min("von_Revised").as("vonMin")
                .avg("von_Revised").as("vonAvg")
                .sum("von_Revised").as("vonSum")

                .last("voff_Revised").as("voffLast")
                .max("voff_Revised").as("voffMax")
                .min("voff_Revised").as("voffMin")
                .avg("voff_Revised").as("voffAvg")
                .sum("voff_Revised").as("voffSum")

                .last("vac").as("vacLast")
                .max("vac").as("vacMax")
                .min("vac").as("vacMin")
                .avg("vac").as("vacAvg")
                .sum("vac").as("vacSum")

                .last("von2").as("von2Last")
                .max("von2").as("von2Max")
                .min("von2").as("von2Min")
                .avg("von2").as("von2Avg")
                .sum("von2").as("von2Sum")

                .last("vac2").as("vac2Last")
                .max("vac2").as("vac2Max")
                .min("vac2").as("vac2Min")
                .avg("vac2").as("vac2Avg")
                .sum("vac2").as("vac2Sum")

                .last("battery").as("batteryLast")
                .max("battery").as("batteryMax")
                .min("battery").as("batteryMin")
                .avg("battery").as("batteryAvg")
                .sum("battery").as("batterySum")

                .count().as("total")
);
AggregationResults<MessageHistoryA_In3Month> aggregate = mongoTemplate.aggregate(aggregation, this.getCollectionName(), this.getEntityClass());
//通过调试可知道,查询出的结果存放在aggregate.getRawResults().get("result")中;
BasicDBList basicDBList = (BasicDBList) aggregate.getRawResults().get("result");
return basicDBList;
更改状态以发布