Chapter 1 optional lab

Not sure what I’m doing wrong here, or what I should be searching for to find an answer (or if I’m completely on the wrong track)

I have a projection that removes anything in parentheses via $map on the writers, cast and directors arrays, so the next stage would be to do a $match to filter the results down to documents where the writers array contains elements from cast and directors. I’ve tried using $ememMatch and $in but I’m seeing errors suggesting I’m not querying the output from the previous stage.

When the final aggregation stage is:

                    $in: cast,
                    $in: directors

I get the error : ReferenceError: cast is not defined. Similarly if I change the cast to $cast.

If I change to "$cast" I get $in needs an array

What’s my misconception?

@ Jacques_52247

First of all, please do not post code in the Forum. There used to be a post to that effect from Jose Carlos, but I see that’s gotten buried (for me, anyway) down in the other posts. In any case, I’ve slightly edited your post to remove the full code example.

Second, I would recommend that you review the schema as displayed when you use the findOne() indicated. What result will you get if you split the “cast” field on the string ’ (’? Overall, I think you’ve probably over-complicated your query. Good luck.

Sure, I can simplify that if I assume there’s no parentheticals in cast or director but you’ve failed to address my actual query of how I do an $elemmatch in an array that’s an output of a previous aggregation stage, or even in the original document. Am I getting the syntax wrong, or is the idea of comparing two arrays like this not something that Mongo supports?

The syntax for $in is documented in

In short it is { $in: [ <expression>, <array expression> ] }

So it needs an array of 2 elements. The first one is an expression and the second one being an array.

Examples are provided in the documentation.

@ Jacques_52247

Sorry, but that was not at all what I got out of your original post which seemed much more complex than your revised question here. I would recommend to answer your question that you review the documentation on $elemMatch here which does in fact not look at all like your use here. Good luck.

I worked this out in the end. The missing element was the $setIntersection operator, which let me get the documents where there was at least one common element in the writers, directors and cast arrays

When you use “$cast”, the error comes because in db.movies collection some of the documents do not have cast field so $in get a null value.

Your solution using $setIntersection is OK. I just want to explain the error.

I used a $match like this
{ $match: {
“labor of love.0”: { $exists : true }
to return only documents with a non-null array–at least one all-rounder director, writer, actor.
I get 1259 documents.
The solution uses two $project stages, the first using the $split of the writer array to $map a revised writer array.
When I remove that $project stage I get the same as my response. I don’t understand why removing this stage would ADD to the number of “all-rounders” or “labor_of_love” found. Is there an explanation?
I’ve perused the output from both with and without that project stage by including an output field of “all rounders” in each case and I can’t see any obvious difference. I thought maybe there would be blank element arrays in the Solution but can’t see any.

Please pardon my failure to grasp the difference in results. Of course if I don’t strip the parenthetic from the elements in the writer fields, those elements won’t match up when the $setIntersection operator is applied. That’s why I getting a low count!

I had an $exists match at the start, but of course that only filters out cases where the array doesn’t exist at all, not where it does exist but is null… That’s why the model answer has $elemmatch instead

Hi, to be honest right now I’m lost to know how do it the $match, I could clean the three elements on my aggregate, directors, writers and cast but now I don’t know how to do to check if the elements are equal :cold_sweat: