Pipeline in lookup aggregation not working in mongodb
17,389
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"
}}
])
Related videos on Youtube
Author by
KitKatKot
Updated on June 23, 2022Comments
-
KitKatKot almost 2 years
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" }
I want to join those and before doing so, I want to filter out the
Shop
s which do not have theshopPosId
field.Here's my code:
Compte.aggregate([ { $match: { $and:[{"clientUid":clientUid}] } }, { $lookup: { from: "Shop", localField: "shopId", foreignField: "shopId", let: {"Shop.shopPosId": "$shopPosId"}, pipeline: [{$match: {"shopPosId": {"$exists": false}}}], as: "shopDescr" } }] );
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).Is this because the two collections have the
shopPosId
field? (if so, isn't this linelet: {"Shop.shopPosId": "$shopPosId"}
supposed to take care of it ?)-
Alex Blex almost 6 yearsYou are mixing $lookup syntax. It is either
from-localField-foreignField-as
orfrom-let-pipeline-as
, but not both together.
-
-
KitKatKot almost 6 yearsOk, this is informative, thanks! Still, I get an
undefined
result, I'd assume it's to do with my data except I really don't think so. Is it necessary to have mongoDB 3.6 to be able and apply a pipeline in a lookup ? -
KitKatKot almost 6 yearsYeah, seems like it is, confirmed in the officiel documentation: docs.mongodb.com/manual/reference/operator/aggregation/lookup/…