Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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:
-----------------------------------------------------------------------
-----------------------------------------------------------------------
"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 the shopPosId
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 line let: {"Shop.shopPosId": "$shopPosId"}
supposed to take care of it ?)
–
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"
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.