可能在$connectToField中添加$cond示例
{ $graphLookup:{ from:'samecollection',startWith:'$myid',connectFromField:'myid',connectToField:{ $cond:{ true,'myidsRelated.id','newRelated.id'} },as:'elements',},},
发布于 2020-10-13 22:46:11
不,这不可能。connectToField参数仅允许您指定要联接到的字段的名称。它不支持表达式(这就是指定字段名称时不使用$前缀的原因)。
但是,有一个restrictSearchWithMatch选项可用于过滤要参与查找的文档。例如:
{
$graphLookup: {
from: "somecollection",
startWith: "$myid",
connectFromField: "myid",
connectToField: "newRelated.id",
as: "elements",
restrictSearchWithMatch: { "newRelated.id": { $exists: true } }
}
}https://stackoverflow.com/questions/64336796
复制相似问题