Skip to content

Commit 2d33f24

Browse files
committed
Allow $lookup with pipeline
1 parent 6b84527 commit 2d33f24

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/server/data_form.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,23 +1449,35 @@ export class FormsAngular {
14491449
case "$graphLookup":
14501450
let needFindFunc = true;
14511451
if (keys[0] === "$lookup") {
1452-
// For now at least, we only support simple $lookups with a single join field equality
14531452
let lookupProps = Object.keys(stage.$lookup);
1453+
// First deal with simple $lookups with a single join field equality
14541454
if (
1455-
lookupProps.length !== 4 ||
1456-
lookupProps.indexOf("from") === -1 ||
1457-
lookupProps.indexOf("localField") === -1 ||
1458-
lookupProps.indexOf("foreignField") === -1 ||
1459-
lookupProps.indexOf("as") === -1
1455+
lookupProps.length === 4 &&
1456+
lookupProps.includes("from") &&
1457+
lookupProps.includes("localField") &&
1458+
lookupProps.includes("foreignField") &&
1459+
lookupProps.includes("as")
14601460
) {
1461+
// If we are doing a lookup using an _id (so not fishing) we don't need to do the findFunc (see tkt #12399)
1462+
if (stage.$lookup.foreignField === "_id") {
1463+
needFindFunc = false;
1464+
}
1465+
} else if (
1466+
lookupProps.length === 4 &&
1467+
lookupProps.includes("from") &&
1468+
lookupProps.includes("let") &&
1469+
lookupProps.includes("pipeline") &&
1470+
lookupProps.includes("as")
1471+
) {
1472+
// can't think of a way to exploit this (if you add a findFunc) but it is the responsibility of the user
1473+
// to ensure that the pipeline returns fields used by the findFunc
1474+
console.log(`In some scenarios $lookups that use pipelines may not provide all the fields used by the 'findFunc' of a collection.
1475+
If you get no results returned this might be the explanation.`)
1476+
} else {
14611477
throw new Error(
1462-
"No support for $lookup that isn't Equality Match with a Single Join Condition"
1478+
`No support for $lookup of with properties ${lookupProps.join(', ')}`
14631479
);
14641480
}
1465-
// If we are doing a lookup using an _id (so not fishing) we don't need to do the findFunc (see tkt #12399)
1466-
if (stage.$lookup.foreignField === "_id") {
1467-
needFindFunc = false;
1468-
}
14691481
}
14701482
// hide any hiddenfields in the lookup collection
14711483
const collectionName = stage[keys[0]].from;

0 commit comments

Comments
 (0)