Aggregation using MongoDB java driver with multiple $project

喜夏-厌秋 提交于 2020-01-06 05:25:37

问题


I am trying to convert Mongo Aggregation query into Java and I am kind of stuck on the second $project statement

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"
    }
])

Any help is really appreciated. TIA

Java Code -

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

I am getting error on "$$x.version" such as - Unrecognized expression '$$x.version'. The full response is { "ok" : 0.0, "errmsg" : "Unrecognized expression '$$x.version'", "code" : 168, "codeName" : "InvalidPipelineOperator" }

来源:https://stackoverflow.com/questions/51430644/aggregation-using-mongodb-java-driver-with-multiple-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!