我正在尝试根据之前的查找在查找中进行搜索,但没有成功。
我得到了我应该得到的所有结果,但是office总是返回一个空数组,这里我遗漏了什么?
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"
}
};
发布于 2018-01-31 03:47:30
使用$arrayElemAt访问上一个查找数组中的元素。
试一试
let: {elem: {$arrayElemAt:["$sales_agent", 0]}} &
{$match: {$expr: {$eq: ['$_id', '$$elem.office']}}}或在一次修改中
let: {office: {$let:{vars:{elem:{$arrayElemAt:["$sales_agent", 0]}}, in:"$$elem.office"}}}https://stackoverflow.com/questions/48525150
复制相似问题