设置差异给出不正确的结果
Setdifference giving incorrect results
我们正在尝试打印 2 个数组的差异,从不同的集合中检索为 below.However,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 "]] }]
这里的问题是您在使用未被访问的 setDifference 时尝试访问数组中的字段,而只是按原样返回全局脚本,请尝试在 setdifference
中添加 arrayelemat
{
$project: {
unsent: {
$setDifference: [
{
$arrayElemAt: [
"$request.scrips",
0
]
},
"$global"
]
}
}
}
给出输出:
[
{
"_id": null,
"scrips": [
"1234"
]
}
]
我们正在尝试打印 2 个数组的差异,从不同的集合中检索为 below.However,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 "]] }]
这里的问题是您在使用未被访问的 setDifference 时尝试访问数组中的字段,而只是按原样返回全局脚本,请尝试在 setdifference
中添加 arrayelemat {
$project: {
unsent: {
$setDifference: [
{
$arrayElemAt: [
"$request.scrips",
0
]
},
"$global"
]
}
}
}
给出输出:
[
{
"_id": null,
"scrips": [
"1234"
]
}
]