M121 Chapter 3 Lab - Using $lookup: Seeking clarification on array-scalar comparisons and variable syntax

I was able to get the right answer, but my solution was much more complicated than the “official” solution.

The main reason why mine is so much more complicated is because, in the beginning, I was having trouble trying to join air_alliances to air_routes, and I mistakenly assumed this was because the localField, airline.name, was a scalar while the foreignField, airlines, was an array, so I devised an overly complicated workaround that involved unwinding the “airlines” arrays in air_alliances in a pipeline within lookup.

I obviously didn’t need to do this. The lecture video clearly shows an example of a situation where the localField is a scalar and the foreignField is an array.

What I’m confused about is the part in the lecture video where it is stated that MongoDB does a “strict equality comparison” between the localField and the foreignField.

Would a “strict equality comparison” be equivalent to $eq? Because, in the documentation for $eq, we see the following:

{ <field>: { $eq: <value> } }

If the specified <value> is an array, MongoDB matches documents where the <field> matches the array exactly or the <field> contains an element that matches the array exactly. The order of the elements matters.

A scalar can’t match an array exactly, so I hope you understand why I’m confused.

To further add to my confusion, the page in the documentation for $lookup states:

If the localField is an array, you can match the array elements against a scalar foreignField without an $unwind stage.

However, it does not appear to explicitly state anything about the case where the localField is a scalar and the foreignField is an array (even though, as I stated earlier, the lecture video on $lookup demonstrates that case).

So are we safe to assume that $lookup really doesn’t always perform a “strict equality comparison” in every case? That, if one is an array and one is a scalar, regardless of which is the localField and which is the foreignField, MongoDB will always check whether the scalar is contained in the array?

Also, in the pipeline within $lookup, I had success with $expr and $eq but not with trying to use a variable directly (the commented-out line).

let: { name: "$airline" },
pipeline: [
  ...
  {
    $match: {
      $expr: { "$eq": ["$airline", "$$name"] }
      //airline: "$$name"
    }
  }

I’m having a bit of trouble understanding the rules about variable syntax in pipelines within pipelines. In the lecture video on $reduce, we were shown the $$this and $$value variables, but I’m wondering if there’s a page in the documentation somewhere with more generalized rules.

Thank you in advance for your help.

Hi @J_A,

That’s slightly incorrect, I will forward this to the appropriate team, and our Curriculum Engineers will make the required changes needed to make it more clear.

Yes, that’s true, a simple lookup uses $eq.

Exactly, that’s absolutely correct.

When you are referring to a field name from the documents that are being passed from the previous stage, you should use a single $ sign like $fieldName and when you want to refer to the variable you created in that particular stage you should use double $ sign like $$variableName.
I hope that helps.

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

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

1 Like

Thank you very much for your help!