Trying to return a range and sort results

Hi I’m trying to only show rating between 7 and 10. The rating part by itself works fine. If I want to sort it it’s not working.
[{rating: {$gt:6, $lt: 10}}, {$sort: {rating: -1}}]

// // Select the database to use.

// Insert a few documents into the movies collection.
{"_id": 1, “title”: “First Movie”, “year”: “2000”, “rating”: 10, “Genre”: “Drama”, “language”: “English”},
{"_id": 2, “title”: “Second Movie”, “year”: “2001”, “rating”: 9, “Genre”: “Thriller”, “language”: “English”},
{"_id": 3, “title”: “Third Movie”, “year”: “2002”, “rating”: 9, “Genre”: “Comedy”, “language”: “English”},
{"_id": 4, “title”: “Fourth Movie”, “year”: “2020”, “rating”: 8, “Genre”: “Drama”, “language”: “Russian”},
{"_id": 5, “title”: “Fifth Movie”, “year”: “2004”, “rating”: 1, “Genre”: “Drama”, “language”: “Russian”},

Hello @Dmitriy_Malayev, Welcome to the MongoDB Community forum!

The query method db.collection.find takes two parameters - the filter and the projection. Then, the query returns a cursor. You can apply various methods (or operations) on this cursor. You can apply the sort operation on the cursor.

It looks like you are trying to apply the sort within the find method. That is the reason the sort did not work!

1 Like