@@ -1391,6 +1391,34 @@ export class FormsAngular {
1391
1391
if ( findFuncQry ) {
1392
1392
retVal . unshift ( { $match : findFuncQry } ) ;
1393
1393
}
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
+
1394
1422
for (
1395
1423
let pipelineSection = 0 ;
1396
1424
pipelineSection < array . length ;
@@ -1406,6 +1434,7 @@ export class FormsAngular {
1406
1434
case "$addFields" :
1407
1435
case "$count" :
1408
1436
case "$group" :
1437
+ case "$unset" :
1409
1438
case "$limit" :
1410
1439
case "$replaceRoot" :
1411
1440
case "$replaceWith" :
@@ -1461,15 +1490,21 @@ export class FormsAngular {
1461
1490
let lookupProps = Object . keys ( stage . $lookup ) ;
1462
1491
// First deal with simple $lookups with a single join field equality
1463
1492
if (
1464
- lookupProps . length === 4 &&
1493
+ ( lookupProps . length === 4 || lookupProps . length === 5 ) &&
1465
1494
lookupProps . includes ( "from" ) &&
1466
1495
lookupProps . includes ( "localField" ) &&
1467
1496
lookupProps . includes ( "foreignField" ) &&
1468
1497
lookupProps . includes ( "as" )
1469
1498
) {
1470
1499
// 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" ) ;
1473
1508
}
1474
1509
} else if (
1475
1510
lookupProps . length === 4 &&
@@ -1478,30 +1513,7 @@ export class FormsAngular {
1478
1513
lookupProps . includes ( "pipeline" ) &&
1479
1514
lookupProps . includes ( "as" )
1480
1515
) {
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 ) ;
1505
1517
} else {
1506
1518
throw new Error (
1507
1519
`No support for $lookup of with properties ${ lookupProps . join ( ', ' ) } `
0 commit comments