Using $graphlookup to replace $match & $lookup

Hi im just wrapping up M121 and working on the last question. I am trying to use graphlookup but am just getting an empty array. I was able to run $match and $lookup independent and get back routes with correct src/dst and their alliance/ Can it be done with just $graphLookup?

db.air_routes.aggregate([
  {$graphLookup: {
    from: 'air_alliances',
    startWith: '$airlines',
    connectFromField: 'airlines',
    connectToField: 'airline.name',
    as: 'carriers',
    maxDepth: 0,
    restrictSearchWithMatch: {
      src_airport: {$in: ["LHR", "JFK"]},
      dst_airport: {$in: ["LHR", "JFK"]}
    },
  }},
])

I do not know if it is feasible with $graphLook but at first glance one of the issue that you have is that the restrictSearchWithMatch: condition is applied to the looked up documents from the from: collection. The air_alliances has not field named src_airport or dst_airport.

The connectToField must be a field of documents in the looked up collections.

1 Like

probably not feasible at all but I will try and make it work at some point just for fun haha. Thx for pointing that out. Idk why it becomes so hard to think about when plugging the values in >.<

1 Like