Lab $Lookup - how to match or filter

Hello everyone,
I’ve got myself completely stuck with this lab. Logically it appears simple enough, but I’m struggling to get any sensible output.
Can anyone point me to some resources that can help me out, please?
I’m struggling with the filter stage. In particular, I can’t seem to find a sensible way to reach into the new (very large) “airlines” array and extract only the relevant documents.
I’ve read through " Query for a Document Nested in an Array", I’ve tried $match and $filter, but nothing is working and I’m completely at a loss how to do this.
Any advice would be most welcome.
Thank you.

@astebbing

Well, let’s see if I can help without giving too much away. First of all, notice that ‘airlines’ in the air_alliances collection is an array of strings, and not of documents. So you want to use “Query an Array” – there are examples here – and not “Query for a Document Nested in an Array”.

Then notice that you simply query inside an array of strings with a simple standard query, like

db.air_alliances.find( { "airlines": "Avianca" } )

Also consider that you are cross-referencing two collections here, correct? (That’s the whole point of $lookup) and you want to consider which two collections will give you the information you need. Then think about which one to start with, how to pare that one down to the documents with relevant information, and then cross-reference that to the other collection using $lookup. Good luck.

Thank you @DHz.

I did, in fact, really want to query a field inside a document inside an array as I ended up with an array of documents and needed to filter them on an embedded field.

After many hours of trial and error, I actually got the right answer; however, having now seen the ‘official’ solution I can see why my question doesn’t make sense. I’ve joined the documents the wrong way round!

That said unless I missed something in the lecture (and as a hint for others who may get themselves in a mess like me) the official manual page for $lookup doesn’t mention that you can match a single value in one collection vs an array containing that value in another collection. Therefore, it never occurred to me to start the same way as the ‘official’ solution. I took the example given in the lecture as a pattern to follow.

@astebbing

Well, the documentation does say you can use any equality comparison; and as you could see from the example I gave you, a query for a string value inside an array of strings is a simple equality comparison. Not intrinsically obvious I know, but now you know too. :wink:

Thanks, @DHz, I have learnt quite a bit from struggling with this exercise.
I don’t find $lookup and $unwind with $group easy to grasp. I’m not sure that, even having seen the ‘official’ solution, I could logically deduce the steps needed to get from the initial $lookup stage to reach the final result; I can’t visualise what structure the data is going to have as it flows thorough the pipeline.
I sense the final exam tasks may prove quite a challenge.