Chapter 2 Lab 1

Hi,

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

I did not get any answer or error it just finished processing and shows again PRIMARY>

Please help me for this.

1 Like

Hint

This command produces no output either:

db.movies.find({country:{$exists:true}})
2 Likes

I did not understand hint. Can you explain me where code is wrong? Is there something missing to execute program?

db.movies.aggregate([
{$match: {“tomatoes.viewers.rating”: {$gte: 3}, “countries”: {$in: [“USA”]}, “cast”: {$exists: true}}},
{$addFields: {“num_favs”: {$size: {$setIntersection: ["$cast", [“Sandra Bullock”, “Tom Hanks”, “Julia Roberts”, “Kevin Spacey”, “George Clooney”]]}}}},
{$sort: {“num_favs”: -1, “tomatoes.viewers.rating”: -1, “title”: -1}},
{$skip: 24}, {$limit: 1}
])

You did quite well.

But tomatoes.viewers.rating does not exist either. The point here is, carefully inspect the data, it will be useful in and out of the course.

Use findOne(), find().limit(), $sample, Compass or Atlas. Or any other way you find useful.

$in:["USA"] is the same as just "USA". $in is useful for multiple values. You may test this because I can be confused.

1 Like

You did not but you still changed the field name from country to countries.

1 Like