Chapter 2: Lab Using Curson-like Stages

I’m not sure what is meant by the the instruction below:

“calculate a new field called num_favs that represents how many favorites appear in the cast field of the movie.”

Can you give an example?

Although not discussed, should we use “unwind” in the solution?

@ SgtJoker

Well, find the number of movies in the collection that have at least one of the “favorites” in the cast. Then compare the cast in each of those to the “favorites” to generate a number of favorites in each movie – basically, the number of actors in the cast that also appear in the “favorites”. If you make each of those an array, you’ll be able to do that pretty easily. You can assign that value to a new field “num_favs” which will give you the desired result.

And no, you do not need to use $unwind to solve this.

It was strange that the right answer was in the 26th position, not 25th.

I included skip(24) in my solution and got the correct answer, so it seemed to be the 25th for me…

Maybe, I’ve checked the result with using {$skip: 24} and the right answer was in the second position.
I don’t know why.
I don’t think it’s important :). I answered correctly the first time.

Sorry,

I try to do the exercise. I have some problem with
“calculate a new field called num_favs that represents how many favorites appear in the cast field of the movie”.

Is it possible to the exercise only with the cursor like as $count and $size as well as with $match,$project and $addFields? With $match I filtered only the document where there are some favorites actors. But my problem is that I don’t know how count how many favorites actors there are…I try use the aggregator $filter but the stages wasn’t in the videos…

I think I’m missing something…

I’m trying to get this to work with $elemMatch, but have been unsuccessful so far. Am I on the right track?

Well, this one not so hard to do:

but I have no idea how to do this one:

How we can to get an filed with neccasary actors only? Not all actors, but only that present in the “favorites” array. That’s a big question for me.
Could anybody helps me? @asevostyanov @Matt_75835 @DHz please!

Sorry, I found solution.
The $setIntersection operator was helpfull to me.

I used $in which got me further.

@alexlab, yea, I used $setIntersection also. I stumbled upon it figuring out the optional lab in chapter 1… had to pat myself on the back after that one.

@Matt_75835 & @alexlab

Yes, $setIntersection is a good choice. (at least, it’s the one I used :wink: )

But I cannot $setIntersection with “$cast” and “$favorites”. I have something like this:

{
$project: {
.
.
cast: 1,
favorites: [“Sandra Bullock”, “Tom Hanks”, “Julia Roberts”, “Kevin Spacey”, “George Clooney”]
}
}, {
$addFields: {

cast_size: {
$size: “$cast”
}

I’m getting error: The argument to $size must be an array, but was of type: missing

Also, commonfav below is getting empty array:
{
$addFields: {

    commonfav: {
        $setIntersection: ["$cast", "$favorites"]
    }
}

Please help!

Never mind. I figured it out by checking the value of “cast”. Thanks!

For example, you should use $split for creating array of favorites and then use $in to compare with “$cast”

Please follow forum guidelines and to not post potential answer.

Do you have a question?