Reputation: 3264
I'm trying to search in lookup based on the previous lookup but no luck.
I get all the results I'm supposed to get but office is returning always an empty array what am i missing here?
query[2] = {
$lookup: {
from: "users",
let: {sales_agent: "$sales_agent"},
pipeline: [
{$match: {$expr: {$eq: ['$_id', '$$sales_agent']}}},
{$project: {_id: 1, username: 1, office: 1}},
],
as: "sales_agent"
}
};
query[3] = {
$lookup: {
from: "offices",
let: {office: "$sales_agent.0.office"}, //this value supposed to be come from the previous lookup
pipeline: [
{$match: {$expr: {$eq: ['$_id', '$$office']}}},
{$project: {_id: 1, city: 1,}},
],
as: "office"
}
};
Upvotes: 2
Views: 1153
Reputation: 75934
Use $arrayElemAt
to access the element from previous lookup array.
Try
let: {elem: {$arrayElemAt:["$sales_agent", 0]}}
&
{$match: {$expr: {$eq: ['$_id', '$$elem.office']}}}
or in one modification
let: {office: {$let:{vars:{elem:{$arrayElemAt:["$sales_agent", 0]}}, in:"$$elem.office"}}}
Upvotes: 1