Chapter 2: Basic Aggregation - Utility Stages Lab: Using Cursor-like Stages. Incorrect answer

I tried the below two aggregate pipelines,

Pipeline 1:
db.movies.aggregate([
{"$match": {“type”: “movie”, “countries”: {"$in": [“USA”]}, “tomatoes.viewer.rating” : {"$gte": 3.0}, “cast”: {"$ne": null} }},
{"$project": {“title”: 1, “cast”: 1, “_id”:0, “rating”:"$tomatoes.viewer.rating"}},
{"$addFields": {“num_favs”: {"$setIntersection": [[“Sandra Bullock”,“Tom Hanks”,“Julia Roberts”,“Kevin Spacey”,“George Clooney”],"$cast"]}}},
{"$addFields": {“num_favs”: {"$size": “$num_favs”}}},
{"$sort": {“num_favs”:-1, “rating”: -1,“title”: -1}},
{"$skip": 24},
{"$limit": 1},
]);

Pipeline 1 Result: Recount

Pipeline 2:

db.movies.aggregate([
{"$match": {“type”: “movie”, “countries”: {"$in": [“USA”]}, “tomatoes.viewer.rating” : {"$gte": 3},“cast”: {"$in": [“Sandra Bullock”,“Tom Hanks”,“Julia Roberts”,“Kevin Spacey”,“George Clooney”]} }},
{"$project": {“title”: 1, “cast”: 1, “_id”:0, “rating”:"$tomatoes.viewer.rating"}},
{"$unwind": {“path”: “$cast”}},
{"$addFields": {“num_favs”: {"$cond": { “if”: { “$in”: [ “$cast”, [“Sandra Bullock”,“Tom Hanks”,“Julia Roberts”,“Kevin Spacey”,“George Clooney”] ] }, then: 1, else: 0 } }}},
{"$group": {"_id": {“title”: “$title”}, “num_favs”: {"$sum": “$num_favs”}, “rating” : {"$first": “$rating”}, “cast”: {"$addToSet": “$cast”} }},
{"$project": {“title”: “$_id.title”, “num_favs”: 1, “rating”: 1, “cast”: 1}},
{"$sort": {“num_favs”:-1, “rating”: -1,“title”: -1}},
{"$skip": 24},
{"$limit": 1},
{"$project": {“cast”: 1, “title”: 1}}
]);

pipeline 2 result: Recount

However the answer is incorrect. Can you please suggest me if I’m missing anything specific to get the desired result?

1 Like

Hi @Laks,
Please note that the question asks the following:

For movies released in the USA with a tomatoes.viewer.rating greater than or equal to 3, calculate a new field called num_favs that represets how many favorites appear in the cast field of the movie.

Sort your results by num_favs, tomatoes.viewer.rating, and title, all in descending order.

What is the title of the 25th film in the aggregation result?

All documents in the movies collection are considered as individual movies, therefore you don’t have to filter documents with type: "movie".
Also, this won’t make a difference, but in my opinion, whenever you are filtering documents based on a single value and not a group(array) of values please avoid using $in it, it will make your code a lot more readable.
Considering all the suggestions I mentioned above, your first pipeline looks good to me, except for the cast filter, use the cast filter mentioned in your second pipeline instead:

"cast": {"$in": ["Sandra Bullock","Tom Hanks","Julia Roberts","Kevin Spacey","George Clooney"]

One last feedback that I would like to give is, please utilize variables for more readability.
For eg.:
Since the cast array with all 5 names in it appears multiple times in the pipeline, therefore you can declare it once and reference it multiple times in your pipeline.

var favorites = [
  "Sandra Bullock",
  "Tom Hanks",
  "Julia Roberts",
  "Kevin Spacey",
  "George Clooney"]
db.movies.aggregate([
  {
    $match: {
      "tomatoes.viewer.rating": { $gte: 3 },
      countries: "USA",
      cast: {
        $in: favorites <--- Notice this
      }
    }
  },
...

I hope it helps. All the best :green_heart: .

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

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

Thanks Sourabh. This was helpful.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.