这是我的Mongo DB聚合查询: db.EmployeeResults.aggregate([
{
$match: {
"_id.employeeID" : "e2c42da3-1bfa-49fe-9a3d-6528af45c54d",
"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"
}
])
上面的Java查询写为:
AggregationOutput agg1 = db.getCollection("EmployeeResults").aggregate(
new BasicDBObject().append("$match",
new BasicDBObject().append("_id.employeeID", "e2c42da3-1bfa-49fe-9a3d-6528af45c54d").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", Arrays.asList("$$this.version", "$MaxVersion")))))),
new BasicDBObject().append("$unwind", "$Versions"),
new BasicDBObject().append(
"$project",
new BasicDBObject().append("errors", "$Versions.errors")
)
);
我试图将Java中的同一聚合查询转换为Groovy :(基本上将所有与mongo相关的表达式替换为单引号“ $ project”-'$ project')
AggregationOutput agg1 = db.getCollection("EmployeeResults").aggregate(
new BasicDBObject().append('$match',
new BasicDBObject().append("_id.employeeID", "e2c42da3-1bfa-49fe-9a3d-6528af45c54d").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', asList('$$this.version', '$MaxVersion')))))),
new BasicDBObject().append('$unwind', '$Versions'),
new BasicDBObject().append('$project',
new BasicDBObject().append("errors", '$Versions.errors'))
)
我得到零行,因为在$ filter“ cond”中找不到正确的内容,无法在$$ this.version中找到MaxVersion。 我的Java程序返回18行,但是上面的常规代码给了我0行。 当我尝试在Groovy程序中注释查询的最后2行时,它向我返回行 很感谢任何形式的帮助。预先感谢。