【发布时间】:2018-12-11 22:30:49
【问题描述】:
我是 mongodb 的新手,所以我希望这不会成为一个非常基本的问题。我做了一些研究并试图应用我发现的东西,但似乎有些东西让我无法理解。
我有两个以下格式的集合:
-----------------------------------------------------------------------
Shop
-----------------------------------------------------------------------
{
"shopId": "1002",
"shopPosId": "10002",
"description": "some description"
}
-----------------------------------------------------------------------
Compte
-----------------------------------------------------------------------
{
"shopId": "9000",
"shopPosId": "0000",
"clientUid": "474192"
}
我想加入这些,在加入之前,我想过滤掉没有shopPosId 字段的Shops。
这是我的代码:
Compte.aggregate([
{
$match:
{
$and:[{"clientUid":clientUid}]
}
},
{
$lookup:
{
from: "Shop",
localField: "shopId",
foreignField: "shopId",
let: {"Shop.shopPosId": "$shopPosId"},
pipeline: [{$match: {"shopPosId": {"$exists": false}}}],
as: "shopDescr"
}
}]
);
返回的结果是undefined,这意味着查询没有多大意义(因为实际上我至少应该得到一个空数组)。
这是因为这两个集合都有shopPosId 字段吗? (如果是这样,这条线let: {"Shop.shopPosId": "$shopPosId"} 不应该照顾它吗?)
【问题讨论】:
-
clientUid 在你的数据库中吗?
-
是的,只是打字错误,我编辑了帖子,对不起!
-
您正在混合$lookup 语法。它是
from-localField-foreignField-as或from-let-pipeline-as,但不能同时使用。
标签: mongodb mongoose mongodb-query aggregation-framework