使用具有多个$ project的MongoDB Java驱动程序进行聚合

时间:2018-07-19 19:43:44

标签: java mongodb aggregation-framework

我正在尝试将Mongo Aggregation查询转换为Java,并且有点卡在第二个$ project语句中

db.ValidationResults.aggregate([

        {
            $match: { 
                "_id.settlementInstanceId" : "715e66c7-92ff-4619-9324-2c708489fe27",
                "Versions.scope" : "INPUT_QUALITY"

            }
        },
    {       
        $project: {
            Versions: 1,
            MaxVersion: { $max: "$Versions.version" }
        }
    },
    {
        $project: {
            Versions: {
                $filter: {
                    input: "$Versions",
                    as: "x",
                    cond: { $eq: [ "$$x.version", "$MaxVersion" ] }
                }
            }
        }
    },
    {
        $unwind: "$Versions"
    }
])

我们非常感谢您的帮助。 TIA

Java代码-

    AggregationOutput agg1 = db.getCollection("ValidationResults").aggregate(
            new BasicDBObject().append("$match",
                    new BasicDBObject().append("_id.settlementInstanceId", "715e66c7-92ff-4619-9324-2c708489fe27").append("Versions.scope","INPUT_QUALITY")),
            new BasicDBObject().append(
                    "$project",
                    new BasicDBObject().append("Versions", 1).append("MaxVersion",
                            new BasicDBObject().append("$max", "$Versions.version"))),
            new BasicDBObject().append(
                    "$project",
                    new BasicDBObject().append("Versions",new BasicDBObject().append("$filter", new BasicDBObject().append("input", "$Versions").append("cond",
                            new BasicDBObject().append("$eq", new BasicDBObject().append("$$x.version", "$MaxVersion")))))),
            new BasicDBObject().append("$unwind", "$Versions")
                    );

“ $$ x.version”出现错误,例如- 无法识别的表达式'$$ x.version'。完整的响应为{“ ok”:0.0,“ errmsg”:“无法识别的表达式'$$ x.version'”,“ code”:168,“ codeName”:“ InvalidPipelineOperator”}

0 个答案:

没有答案