尝试将mongoDB聚合查询转换为java聚合查询

时间:2017-04-08 10:48:47

标签: java mongodb spring-mongodb

我正在尝试将MongoDB聚合函数转换为Java聚合函数。

我的MongoDB查询是

 BasicDBList subtractDBList= new BasicDBList();
 subtractDBList.add("$indices.dataSets.data");
 subtractDBList.add("$mean");

 BasicDBList multiplyDBList= new BasicDBList();
 multiplyDBList.add(new BasicDBObject("$subtract",subtractDBList));
 multiplyDBList.add(new BasicDBObject("$subtract",subtractDBList));


 Aggregation meanAggregation= Aggregation.newAggregation(Aggregation.match(Criteria.where(IndicesUtil.INDICES_ID).is(indicesId)),
 Aggregation.unwind(IndicesUtil.PREFIX+IndicesUtil.DATA_SETS),
            Aggregation.match(Criteria.where(IndicesUtil.DATA_SETS_DATE).gte(startDate).lte(indicesDataSet.getDate())),
            Aggregation.group(IndicesUtil.INDICES_ID).avg(averageParameter).as(IndicesUtil.MEAN).push("$$ROOT").as("indices"),
            Aggregation.unwind(IndicesUtil.PREFIX+IndicesUtil.INDICES),

            new AggregationOperation() {

                @Override
                public DBObject toDBObject(AggregationOperationContext arg0) {
                    return new BasicDBObject("$project",
                            new BasicDBObject("_id",1).append("mean", IndicesUtil.PREFIX+IndicesUtil.MEAN).append("firstResult", 
                                    new BasicDBObject("$multiply",multiplyDBList)));


                }
            },
            Aggregation.group().avg("$firstResult").as("secondResult").first(IndicesUtil.PREFIX+IndicesUtil.MEAN).as(IndicesUtil.MEAN),
            new AggregationOperation() {

                @Override
                public DBObject toDBObject(AggregationOperationContext arg0) {
                    return new BasicDBObject("$project",
                            new BasicDBObject(IndicesUtil.DATA,
                                    new BasicDBObject("$sqrt","$secondResult").append("mean", IndicesUtil.PREFIX+IndicesUtil.MEAN)));

                }
            }



        );

我在java中试过了转换代码。

 Aggregation.group().avg("$firstResult").as("secondResult").first(IndicesUtil.PREFIX+IndicesUtil.MEAN).as(IndicesUtil.MEAN),

我遇到了bellow line的问题

 Invalid reference '$firstResult'!

以下错误

--packages

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以简化1.10.1-Release spring mongo版本的聚合。您应该避免使用BasicDBObject/Document并使用spring提供的辅助方法。

 Aggregation meanAggregation = Aggregation.newAggregation(
            Aggregation.match(Criteria.where(IndicesUtil.INDICES_ID).is(indicesId)),
            Aggregation.unwind(IndicesUtil.PREFIX+IndicesUtil.DATA_SETS),
            Aggregation.match(Criteria.where(IndicesUtil.DATA_SETS_DATE).gte(startDate).lte(indicesDataSet.getDate())),
            Aggregation.group(IndicesUtil.INDICES_ID).avg(averageParameter).as(IndicesUtil.MEAN).push("$$ROOT").as("indices"),
            Aggregation.unwind(IndicesUtil.PREFIX+IndicesUtil.INDICES),
            Aggregation.project("_id", "mean").andExpression("(indices.dataSets.data - mean) * (indices.dataSets.data - mean)").as("firstResult"),
            Aggregation.group().avg("$firstResult").as("secondResult").first(IndicesUtil.PREFIX+IndicesUtil.MEAN).as(IndicesUtil.MEAN),
            Aggregation.project("mean").and("secondResult").sqrt().as(IndicesUtil.DATA)
 );