admin管理员组文章数量:1391947
I am getting incorrect matched result in $lookup stage. Can you please explain what am I doing wrong?
Please see the following collections and code.
"collection1": [
{
_id: ObjectId("67bf2c061d02c9e2f7978905"),
"pos_v": 111,
"ref_v": "a",
"alt_v": "b",
"gf": 0.2
},
{
_id: ObjectId("67bf2c061d02c9e2f7978908"),
"pos_v": 222,
"ref_v": "a",
"alt_v": "d",
"gf_v": 0.145
}
],
"collection2": [
{
"pos": 111,
"ref": "a",
"alt": "b",
"fieldx": "hdhdhfh"
},
{
"pos": 324,
"ref": "a",
"alt": "s",
"fieldx": "ssdf"
}
]
The query I am running:
db.collection1.aggregate([
{
$match: {
_id: ObjectId("67bf2c061d02c9e2f7978905")
}
},
{
$lookup: {
from: "collection2",
let: {
"pos_gv": "$pos",
"ref_gv": "$ref",
"alt_gv": "$alt"
},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: [
"$$pos_gv",
"$pos_v"
]
},
{
$eq: [
"$$ref_gv",
"$ref_v"
]
},
{
$eq: [
"$$alt_gv",
"$alt_v"
]
}
]
}
}
}
],
as: "existingVariant"
}
},
{
"$unwind": "$existingVariant"
}
])
See here:
Why is the result matching incorrectly? Result:
[
{
"_id": ObjectId("67bf2c061d02c9e2f7978905"),
"alt_v": "b",
"existingVariant": {
"_id": ObjectId("5a934e000102030405000002"),
"alt": "b",
"fieldx": "hdhdhfh",
"pos": 111,
"ref": "a"
},
"gf": 0.2,
"pos_v": 111,
"ref_v": "a"
},
{
"_id": ObjectId("67bf2c061d02c9e2f7978905"),
"alt_v": "b",
"existingVariant": {
"_id": ObjectId("5a934e000102030405000003"),
"alt": "s",
"fieldx": "ssdf",
"pos": 324,
"ref": "a"
},
"gf": 0.2,
"pos_v": 111,
"ref_v": "a"
}
]
Expected Result:
[
{
"_id": ObjectId("67bf2c061d02c9e2f7978905"),
"alt_v": "b",
"existingVariant": {
"_id": ObjectId("5a934e000102030405000002"),
"alt": "b",
"fieldx": "hdhdhfh",
"pos": 111,
"ref": "a"
},
"gf": 0.2,
"pos_v": 111,
"ref_v": "a"
}
]
Did I misunderstand how match works?
本文标签: mongodb match in aggregation pipeline matching incorrect recordsStack Overflow
版权声明:本文标题:mongodb $match in aggregation pipeline matching incorrect records - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744737025a2622392.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论