查找聚合中的管道在 mongodb 中不起作用 [英] Pipeline in lookup aggregation not working in mongodb
问题描述
我是 mongodb 的新手,所以我希望这不会成为一个非常基本的问题.我做了一些研究并尝试应用我发现的东西,但似乎有些东西让我无法理解.
I am new to mongodb so I hope this does not come-off as a very elementary question. I've done some research and tried to apply what I've found but something just seems to escape me.
我有两个以下格式的集合:
I have two collections of the following format:
-----------------------------------------------------------------------
Shop
-----------------------------------------------------------------------
{
"shopId": "1002",
"shopPosId": "10002",
"description": "some description"
}
-----------------------------------------------------------------------
Compte
-----------------------------------------------------------------------
{
"shopId": "9000",
"shopPosId": "0000",
"clientUid": "474192"
}
我想加入这些,在这样做之前,我想过滤掉没有 shopPosId
字段的 Shop
s.
I want to join those and before doing so, I want to filter out the Shop
s which do not have the shopPosId
field.
这是我的代码:
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
,这意味着查询没有多大意义(因为实际上我至少应该得到一个空数组).
the returned result is an undefined
, which means the query doesn't make much sense (because in fact I should at least get a void array).
这是因为两个集合都有 shopPosId
字段吗?(如果是这样,这行不是 let: {"Shop.shopPosId": "$shopPosId"}
应该处理它吗?)
Is this because the two collections have the shopPosId
field? (if so, isn't this line let: {"Shop.shopPosId": "$shopPosId"}
supposed to take care of it ?)
推荐答案
正如 Alex 所说,您正在混合 $lookup
语法在这里...所以正确的是
As Alex commented you are mixing both the $lookup
syntax here... So the correct will be
Compte.aggregate([
{ "$match": { "$and": [{ "clientUid": clientUid }] }},
{ "$lookup": {
"from": "Shop",
"let": { "shopId": "$shopId" },
"pipeline": [
{ "$match": {
"$expr": { "$eq": [ "$shopId", "$$shopId" ] },
"shopPosId": { "$exists": false }
}}
],
"as": "shopDescr"
}}
])
这篇关于查找聚合中的管道在 mongodb 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!