我们正在尝试打印从不同集合中检索的2个数组的差异,但是setdifference似乎不起作用。
怎么了?
预期输出:
{ "request" : [{ "requestId" : "REQ4" }], "unsent" : ["1234"] }
代码摘录和阶段输出如下:
AggregateIterable<Document> diff =
scrips.aggregate(Arrays.asList(
Aggregates.group(null, Accumulators.addToSet("global", "$scrip"))
,Aggregates.lookup("requests",new ArrayList<Bson>(Arrays.asList(new Document(
Document.parse("{\"$match\": { \"requestId\":'" + reqparam + "'}}")))),"request")
[此处输出:{“ _id”:null,“ global”:[“ 3553”,“ 5647”,“ 0001”],“ request”:[{“ _id”:{“ $ oid”:“ 5d6e37db1886a24e70b88b42 “},” requestId“:” REQ4“,” scrips“:[” 3553“,” 5647“,” 1234“]}]}]}}
,Aggregates.project(
Projections.fields(
Projections.excludeId(),
Projections.computed("unsent",
Document.parse("{ $setDifference: [\"$request.scrips\", \"$global\"] }"))
)
)
[在这里输出:{“ request”:[{“ requestId”:“ REQ4”}],“ unsent”:[[“ 3553”,“ 5647”,“ 1234”]]}]
答案 0 :(得分:0)
这里的问题是您正在尝试使用未访问的setDifference来访问数组中的字段,并仅按原样返回全局脚本,请尝试在setdifference中添加arrayelemat
{
$project: {
unsent: {
$setDifference: [
{
$arrayElemAt: [
"$request.scrips",
0
]
},
"$global"
]
}
}
}
给出输出:
[
{
"_id": null,
"scrips": [
"1234"
]
}
]