Lab: Using Cursor-like Stages (Response not in options)

Hello, this is my pipeline:

db.movies.aggregate({$match:{countries: ‘USA’, ‘tomatoes.viewer.rating’:{$gte:3}, cast:{$in:favorites}}}, {$addFields:{num_favs:{$size:{$setIntersection:[favorites,’$cast’]}}}}, {$project:{_id:0, cast:1, countries:1, num_favs:1, ‘tomatoes.viewer.rating’:1, title:1}}, {$sort:{num_favs:-1, ‘tomatoes.viewer.rating’:-1, title:-1}})

The 25th title is ‘Toy Story’. Where is the problem?

Hi @Robert_Europa and welcome to the MongoDB community forums. :wave:

I just ran the code you provided and I get back the correct document. I am seeing Toy Story at position 33. Is it possible you miscounted?

If you put a $skip and $limit stage in your pipeline it’s easier to make sure you looking at the correct document.

1 Like

Are you on the correct database?

M121 uses a read-only access database created for this course. The connection command is given in “Atlas Requirement” lecture in Chapter 0.

I’m using this command:

mongosh “mongodb://,,” --authenticationDatabase admin --tls -u m121 -p aggregations

isn’t it correct?

db.movies.aggregate({$match:{countries: ‘USA’, ‘tomatoes.viewer.rating’:{$gte:3}, cast:{$in:favorites}}}, {$addFields:{num_favs:{$size:{$setIntersection:[favorites,’$cast’]}}}}, {$project:{_id:0, title:1, ‘tomatoes.viewer.rating’:1, num_favs:1}}, {$sort:{num_favs:-1, ‘tomatoes.viewer.rating’:-1, title:-1}}, {$skip:24}, {$limit:1})
title: ‘Toy Story’,
tomatoes: { viewer: { rating: 3.7 } },
num_favs: 1

I’ve do it another once and the same result. I’ve reconnected with the mongosh and not changes.

I have just connected with the above command you posted, and used your query to get the correct result.

are you really using this query on the terminal you used that connection command?

also, did you copy-pasted the “favorites” array correctly?


I am not sure what’s going on. I just copied and pasted the query you have above and ran the query (I had to fix the quotes because code in a paragraph block uses fancy quotes which renders them bad in the mongosh tool) and I get the correct results. I have verified that the connection you posted above is the same that I am using. I am not sure why you’re getting the wrong result.

@SourabhBagrecha, sorry to “at” you here but you work on the University team. Is there anything you can think of that would give two different people different results when running the same query against the same data? To me it doesn’t make any sense. With you being on the team, you might have had other students ask about this as well so I thought I would check with you.

1 Like

Ha! Go with the obvious question. :smiley: I assumed that the favorites array would have been copied from the question, but it could indeed have a typo in it throwing things off.

1 Like

After some quick testing, I can indeed replicate the answer and get Toy Story if I misspell Kevin Spacey’s name.

@SourabhBagrecha no need for you to check in. Sorry for bringing you in directly without first checking the obvious here. :frowning:


You were right. I’m so sorry. There was a mistake in favorites.

var favorites = [ ‘Sandra Bullock’, ‘Tom Hanks’, ‘Julia Roberts’, ‘Kevin spacey’, ‘George Clooney’]

Thank you to you all, and one more time Sorry

1 Like

Hey Robert, no worries. We learn from our mistakes. Nothing to be sorry about. I too made a mistake in assuming the favorites array was created by copy/pasting from the question and not typing it in.

Best of luck to you on the rest of the course, and feel free to keep asking questions. We all learn from others questions.