M121 Chapter 2: Basic Aggregation - Utility Stages Lab: Using Cursor-like Stages

Pipeline:
db.movies.aggregate([
{$match:{cast:{$elemMatch:{$exists:true}},directors:{$elemMatch:{$exists:true}}, writers:
{$elemMatch:{$exists:true}} }},
{$match:{countries:“USA”, “tomatoes.viewer.rating”:{$gte:3}, cast:{$in:favorites}}},
{$set:{num_favs:{$size:{$setIntersection:[favorites, “$cast”]}} }},
{$sort:{num_favs: -1, “tomatoes.viewer.rating”: -1, title: -1}},
{$project:{num_favs:1, “tomatoes.viewer.rating”:1, title:1, _id:0, cast:1, countries:1}}
])
where favorites = [
“Sandra Bullock”,
“Tom Hanks”,
“Julia Roberts”,
“Kevin Spacey”,
“George Clooney”]

I keep getting Recount which is incorrect and I saw The Heat which sits pretty close however I want to achieve the correct answer even if it’s tempting to guess The Heat.
Could you please suggest what’s wrong?

1 Like

+1 for

The first $match stage seems irrelevant to this lab. I see no requirements on directorors or writers. Left over from previous lab may be.

This being out in the open, let me share my favourite $match clause to filter arrays.

// Replace

directors:{$elemMatch:{$exists:true}}

// With

"directors.0":{$exists:true}
1 Like