我是 mongodb 的新手,所以我希望这不会成为一个非常基本的问题。我做了一些研究,并试图应用我所发现的东西,但有些东西似乎让我忽略了。
我有两个以下格式的集合:
-----------------------------------------------------------------------
Shop
-----------------------------------------------------------------------
{
"shopId": "1002",
"shopPosId": "10002",
"description": "some description"
}
-----------------------------------------------------------------------
Compte
-----------------------------------------------------------------------
{
"shopId": "9000",
"shopPosId": "0000",
"clientUid": "474192"
}
Run Code Online (Sandbox Code Playgroud)
我想加入这些,在这样做之前,我想过滤掉Shop没有该shopPosId字段的 s 。
这是我的代码:
Compte.aggregate([
{
$match:
{
$and:[{"clientUid":clientUid}]
}
},
{
$lookup:
{
from: "Shop",
localField: "shopId",
foreignField: "shopId",
let: {"Shop.shopPosId": "$shopPosId"},
pipeline: [{$match: {"shopPosId": {"$exists": false}}}],
as: "shopDescr"
}
}]
);
Run Code Online (Sandbox Code Playgroud)
返回的结果是 an undefined,这意味着查询没有多大意义(因为实际上我至少应该得到一个 void 数组)。
难道是因为两个集合都有字段shopPosId?(如果是这样,这条线不let: {"Shop.shopPosId": "$shopPosId"}应该处理它吗?)