Lab - Using $lookup - Query about initial $match stage


For this lab exercise… I used match stage at the start with or condition as below


But with this in match, I am getting result at end of aggregation as

  { _id: 'OneWorld', count: 10 },
  { _id: 'SkyTeam', count: 8 },
  { _id: 'Star Alliance', count: 5 }

If I change this match stage to {"$match":{“airplane”:/747|380/}} then I am getting proper result as

  { _id: 'SkyTeam', count: 16 },
  { _id: 'OneWorld', count: 15 },
  { _id: 'Star Alliance', count: 11 }

I am trying to understand what is difference between using match with or and match with regular expression. Please help in understanding this. Thanks in advance.

Note: I didint observe spaces at start or end of 747 and 380, atleast for records that I have seen.


Hi @kranthi_kumar1, welcome to the community. :wave: .
That is a really nice question.
Since the question asked us to find:

Which alliance from air_alliances flies the most routes with either a Boeing 747 or an Airbus A380 (abbreviated 747 and 380 in air_routes)

And some of the airplanes have names like:

  • ‘M11 330 747 74M’
  • ‘74M 747’
  • ‘330 777 74M 747’
  • ‘747 74M 777’
  • And sometimes just ‘747’ or ‘380’

We want to find all the routes where the airplane has either ‘747’ or ‘380’ as a part of their name and not just ‘747’ or ‘380’ exactly. Therefore we take the help of regex to check whether ‘747’ or ‘380’ is included anywhere in the airplane name of the concerned document in air_routes collection.

If you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,


Thanks Sourabh… That answers my question…

1 Like