graphLookup question

Related to the graphLookup Lab I have a question on how to use the graphLookup stage…

How would I go about if I wanted to do a graphLookup on the air_routes, but filtered to a particular airline? I.e. I would like to get a list of destinations I can get to from a certain airport using just one particular airline.

I have tried using the restrictSearchWithMatch field,I have tried doing an aggregation on the air_routes themselves and first filtering on airline.name and then doing a graphLookup on that… But somehow I can’t get it to work.

Any hints?

I do not see why restrictSearchWithMatch would not work. Perhaps you can share your graphLookup.

@Alexander_17425

I would start with a $match stage, and then look at the $graphLookup stage. Notice that that would be similar to (but not exactly matching) the $graphLookup Lab. If you have not yet completed that, you might start there. If you have, that will give you someplace to start.

Also, as your question is not directly related to the Lab, feel free to post code as suggested by @steevej-1495 here. Good luck.

Thanks for your feedback!

I think what got me is the Note on restrictSearchWithMatch mentioned on the documentation: https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup/

You cannot use an aggregation expression in this filter

But that’s what I want to do.

So this pipeline gets me halfway there:

[{$match: {
  name: /Homer Air|Far Eastern/
}}, {$graphLookup: {
  from: 'air_routes',
  startWith: "ULN",
  connectFromField: 'dst_airport',
  connectToField: 'src_airport',
  as: 'connections',
  maxDepth: 1,
  depthField: 'no_of_layovers',
  restrictSearchWithMatch: {
    "airline.name": "Far Eastern Air Transport"
  }
}}]

But since I cannot use a variable in the restrictSearchWithMatch, I am a bit at a loss how to get a document for each of the two airlines with their own connections as an array.

When I just leave out the restrictSearchWithMatch, I end up with one document for each airline, each with an array of over 2000 connections from all sorts of airlines.
Even though I guess this is not the most efficient way, I tried to move forward with this result and filter out the unneeded connections.
But even there I failed…

I tried the following without success:

[{$match: {
  name: /Homer Air|Far Eastern/
}}, {$graphLookup: {
  from: 'air_routes',
  startWith: "ULN",
  connectFromField: 'dst_airport',
  connectToField: 'src_airport',
  as: 'connections',
  maxDepth: 1,
  depthField: 'no_of_layovers'
}}, {$unwind: {
  path: "$connections"
}}, {$match: {
  "name": "$connections.airline.name"

  }}]

Looking for further hints how to go about this. Thanks!

When you have 1 document with an array and you would like 1 document for each element of the array you may use https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/

Hi @steevej-1495. Thanks for your feedback.
Please have a look at my pipeline. I am already using $unwind…

Thanks!

I am sorry to suggest $unwind before looking twice at your pipeline.

No worries… :slight_smile:
Thanks again for taking the time to consider my problem!