我想通过以下查询获得预期结果。
我知道$where
无法使用$match
。
我应该如何比较$match
阶段的字符串的每个字段?
查询
db.col_sample.aggregate([
{$unwind: "$array"},
{$lookup: {
from: "col_population",
localField: "array",
foreignField: "foo.key",
as: "join"
}},
{$match: {
"join.foo.key2": "foo.key3"
}}
])
预期结果
{
"_id": (snip),
"foo": {
"key1": "0xBBB",
"key2": "bbb",
"key3": "ccc"
},
"array": [
"0xCCC",
"0xDDD"
]
}
col_population
{
"foo": {
"key1": "0xBBB",
"key2": "bbb",
"key3": "ccc"
},
"array": [
"0xCCC",
"0xDDD"
]
}
{
"foo": {
"key1": "0xCCC",
"key2": "ccc",
"key3": "ddd"
},
"array": [
"0xDDD",
"0xEEE"
]
}
col_sample
{
"foo": {
"key1": "0xAAA",
"key2": "aaa",
"key3": "bbb"
},
"array": [
"0xBBB",
"0xCCC"
]
}
答案 0 :(得分:0)
您需要使用 $redact
管道而不是 $match
,如下所示:
db.col_sample.aggregate([
{$unwind: "$array"},
{$lookup: {
from: "col_population",
localField: "array",
foreignField: "foo.key1",
as: "join"
}},
{$unwind: "$join"},
{$redact: {
$cond: [
{ $eq: [ "$join.foo.key2", "$foo.key3" ] }, /// typo s/sjoin/join/
"$$KEEP",
"$$PRUNE"
]
}}
])