Sort it out!
Want to list movies from the oldest classic to the latest blockbuster? Or maybe highlight the top-rated films first? That's where .sort() comes in.
Use .sort(field, direction) to control the order of your query results:
fieldis the name of the field to sort bydirectionis1for ascending (A-Z, low-high),-1for descending (Z-A, high-low)
This exercise is part of the course
Introduction to MongoDB in Python
Exercise instructions
- List all movies sorted by
release_year(oldest to newest). - List the top 3 highest-rated movies, sorted by
ratingin descending order.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Sort by release year (oldest movies first)
by_year = mov.find().____
print(list(by_year))
# Show the three highest rated movies
by_rating = mov.find().____.____
print(list(by_rating))