Lab: Cursor like stages

db.movies.aggregate([
{
“$match”: {
“countries”: “USA”,
“tomatoes.viewer.rating”: {
“$gte”: 3
},
“cast”: {
“$in”: [
“Sandra Bullock”,
“Tom Hanks”,
“Julia Roberts”,
“Kevin Spacey”,
“George Clooney”
]
}
}
},
{
“$project”: {
“_id”:0,
“title”:1,
“num_favs”: 1,
“favs”: {
“$setIntersection”: [
“$cast”,
[
“Sandra Bullock”,
“Tom Hanks”,
“Julia Roberts”,
“Kevin Spacey”,
“George Clooney”
]
]
}
}
},
{
“$addFields”: {
“num_favs”: {
“$size”: “$favs”
}
}
},
{
“$sort”: {
“num_favs”: -1,
“tomatoes.viewer.rating”: -1,
“title”: -1
}
},
{
“$limit”: 25
}
], {
“allowDiskUse”: true
}).pretty();

I am using above query but I get 25th movie as given below ,

{ “title” : “The War”, “favs” : [ “Tom Hanks” ], “num_favs” : 1 } which is not given in options .
Can any one findout error ?

1 Like

There is a very small mistake in your $project. You forgot something there.

1 Like

Hi, Thanks for your reply

I cann’t find out mistake in $project.
Can you explain in detail or provide hint ?

Directly telling might be a risky. Hint: Please check that are you projecting all the fields which are required in $sort. One is missing.
Thanks