Skip to content

Commit d84465d

Browse files
committed
Add support for another $lookup use case
1 parent 5c600dc commit d84465d

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

src/server/data_form.ts

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,34 @@ export class FormsAngular {
13911391
if (findFuncQry) {
13921392
retVal.unshift({ $match: findFuncQry });
13931393
}
1394+
1395+
async function sanitiseLookupPipeline(stage) {
1396+
// can't think of a way to exploit this (if you add a findFunc) but it is the responsibility of the user
1397+
// to ensure that the pipeline returns fields used by the findFunc
1398+
console.log(`In some scenarios $lookups that use pipelines may not provide all the fields used by the 'findFunc' of a collection.
1399+
If you get no results returned this might be the explanation.`)
1400+
1401+
/* Sanitise the pipeline we are doing a lookup with, removing hidden fields from that collection */
1402+
const lookupCollectionName = stage.$lookup.from;
1403+
const lookupResource =
1404+
that.getResourceFromCollection(lookupCollectionName);
1405+
let lookupHiddenLookupFields = {};
1406+
if (lookupResource) {
1407+
if (lookupResource.options?.hide?.length > 0) {
1408+
lookupHiddenLookupFields = that.generateHiddenFields(
1409+
lookupResource,
1410+
false
1411+
);
1412+
}
1413+
}
1414+
stage.$lookup.pipeline = await that.sanitisePipeline(
1415+
stage.$lookup.pipeline,
1416+
lookupHiddenLookupFields,
1417+
findFuncQry,
1418+
req
1419+
);
1420+
}
1421+
13941422
for (
13951423
let pipelineSection = 0;
13961424
pipelineSection < array.length;
@@ -1406,6 +1434,7 @@ export class FormsAngular {
14061434
case "$addFields":
14071435
case "$count":
14081436
case "$group":
1437+
case "$unset":
14091438
case "$limit":
14101439
case "$replaceRoot":
14111440
case "$replaceWith":
@@ -1461,15 +1490,21 @@ export class FormsAngular {
14611490
let lookupProps = Object.keys(stage.$lookup);
14621491
// First deal with simple $lookups with a single join field equality
14631492
if (
1464-
lookupProps.length === 4 &&
1493+
(lookupProps.length === 4 || lookupProps.length === 5)&&
14651494
lookupProps.includes("from") &&
14661495
lookupProps.includes("localField") &&
14671496
lookupProps.includes("foreignField") &&
14681497
lookupProps.includes("as")
14691498
) {
14701499
// If we are doing a lookup using an _id (so not fishing) we don't need to do the findFunc (see tkt #12399)
1471-
if (stage.$lookup.foreignField === "_id") {
1472-
needFindFunc = false;
1500+
if (lookupProps.length === 4) {
1501+
if (stage.$lookup.foreignField === "_id" || stage.$lookup.localField === "_id") {
1502+
needFindFunc = false;
1503+
}
1504+
} else if (lookupProps.length === 5 && lookupProps.includes("pipeline")) {
1505+
await sanitiseLookupPipeline.call(this, stage);
1506+
} else {
1507+
throw new Error("Unsupported $lookup format");
14731508
}
14741509
} else if (
14751510
lookupProps.length === 4 &&
@@ -1478,30 +1513,7 @@ export class FormsAngular {
14781513
lookupProps.includes("pipeline") &&
14791514
lookupProps.includes("as")
14801515
) {
1481-
// can't think of a way to exploit this (if you add a findFunc) but it is the responsibility of the user
1482-
// to ensure that the pipeline returns fields used by the findFunc
1483-
console.log(`In some scenarios $lookups that use pipelines may not provide all the fields used by the 'findFunc' of a collection.
1484-
If you get no results returned this might be the explanation.`)
1485-
1486-
/* Sanitise the pipeline we are doing a lookup with, removing hidden fields from that collection */
1487-
const lookupCollectionName = stage.$lookup.from;
1488-
const lookupResource =
1489-
that.getResourceFromCollection(lookupCollectionName);
1490-
let lookupHiddenLookupFields = {};
1491-
if (lookupResource) {
1492-
if (lookupResource.options?.hide?.length > 0) {
1493-
lookupHiddenLookupFields = this.generateHiddenFields(
1494-
lookupResource,
1495-
false
1496-
);
1497-
}
1498-
}
1499-
stage.$lookup.pipeline = await that.sanitisePipeline(
1500-
stage.$lookup.pipeline,
1501-
lookupHiddenLookupFields,
1502-
findFuncQry,
1503-
req
1504-
);
1516+
await sanitiseLookupPipeline.call(this, stage);
15051517
} else {
15061518
throw new Error(
15071519
`No support for $lookup of with properties ${lookupProps.join(', ')}`

0 commit comments

Comments
 (0)