Retrieve top 5 movies with highest imdb ratings
Find all movies with title, languages, released, runtime, directors, writers, countries, imdb from the 'movies' collection in MongoDB for the top 5 movies with the highest IMDb ratings.
Structure of 'movies' collection:
{ _id: ObjectId("573a1390f29313caabcd42e8"), plot: 'A group of bandits stage a brazen train hold-up, only to find a determined posse hot on their heels.', genres: [ 'Short', 'Western' ], runtime: 11, cast: [ 'A.C. Abadie', "Gilbert M. 'Broncho Billy' Anderson", 'George Barnes', 'Justus D. Barnes' ], poster: 'https://m.media-amazon.com/images/M/MV5BMTU3NjE5NzYtYTYyNS00MDVmLWIwYjgtMmYwYWIxZDYyNzU2XkEyXkFqcGdeQXVyNzQzNzQxNzI@._V1_SY1000_SX677_AL_.jpg', title: 'The Great Train Robbery', fullplot: "Among the earliest existing films in American cinema - notable as the first film that presented a narrative story to tell - it depicts a group of cowboy outlaws who hold up a train and rob the passengers. They are then pursued by a Sheriff's posse. Several scenes have color included - all hand tinted.", languages: [ 'English' ], released: ISODate("1903-12-01T00:00:00.000Z"), directors: [ 'Edwin S. Porter' ], rated: 'TV-G', awards: { wins: 1, nominations: 0, text: '1 win.' }, lastupdated: '2015-08-13 00:27:59.177000000', year: 1903, imdb: { rating: 7.4, votes: 9847, id: 439 }, countries: [ 'USA' ], type: 'movie', tomatoes: { viewer: { rating: 3.7, numReviews: 2559, meter: 75 }, fresh: 6, critic: { rating: 7.6, numReviews: 6, meter: 100 }, rotten: 0, lastUpdated: ISODate("2015-08-08T19:16:10.000Z") } .....
Query:
db.movies.find({}, {
title: 1,
languages: 1,
released: 1,
runtime: 1,
directors: 1,
writers: 1,
countries: 1,
imdb: 1
}).sort({ imdb: -1 }).limit(5)
Output:
{ _id: ObjectId("573a13faf29313caabdecaf3"), countries: [ 'South Korea' ], runtime: 121, title: 'Right Now, Wrong Then', languages: [ 'Korean' ], released: ISODate("2015-09-01T00:00:00.000Z"), directors: [ 'Sang-soo Hong' ], writers: [ 'Sang-soo Hong' ], imdb: { rating: '', votes: '', id: 4768776 } }, { _id: ObjectId("573a13faf29313caabdeca97"), countries: [ 'Russia' ], runtime: 97, title: 'Tryapichnyysoyuz', languages: [ 'Russian' ], released: ISODate("2015-06-10T00:00:00.000Z"), directors: [ 'Mikhail Mestetskiy' ], writers: [ 'Mikhail Mestetskiy' ], imdb: { rating: '', votes: '', id: 4767340 } }, { _id: ObjectId("573a13faf29313caabdeca48"), countries: [ 'Russia' ], runtime: 84, title: 'Paren s nashegokladbishcha', languages: [ 'Russian' ], released: ISODate("2015-09-03T00:00:00.000Z"), directors: [ 'Anton Chizhikov', 'IlyaChizhikov' ], writers: [ 'Vladimir Seryshev' ], imdb: { rating: '', votes: '', id: 4765708 } }, { _id: ObjectId("573a13faf29313caabdec42e"), countries: [ 'Venezuela' ], runtime: 90, title: 'Desdeallè', languages: [ 'Spanish' ], released: ISODate("2015-09-10T00:00:00.000Z"), directors: [ 'Lorenzo Vigas' ], writers: [ 'Guillermo Arriaga (based on a story by)', 'Lorenzo Vigas (based on a story by)', 'Lorenzo Vigas (screenplay)', 'Lorenzo Vigas (story)' ], imdb: { rating: '', votes: '', id: 4721400 } }, { _id: ObjectId("573a13faf29313caabdec056"), runtime: 87, title: 'Alice in Earnestland', languages: [ 'Korean' ], released: ISODate("2015-08-13T00:00:00.000Z"), directors: [ 'Gooc-jinAhn' ], writers: [ 'Gooc-jinAhn' ], imdb: { rating: '', votes: '', id: 4702528 }, countries: [ 'South Korea' ] }
Explanation:
The said query in MongoDB returns movies with the title, languages, released, runtime, directors, writers, countries, and imdb fields from the 'movies' collection in MongoDB that retrieves the top 5 movies with the highest IMDb ratings.
The second parameter of the find method assigned a value of 1 for the fields, indicates the inclusion of the fields title, languages, released, runtime, directors, writers, countries, and imdb in the output.
The sort method sorts the retrieved documents in descending order based on the imdb field.
The limit method restricts the result to only the top 5 documents.
Improve this sample solution and post your code through Disqus.
Previous: Movies with runtime between 60 and 90 minutes.
Next: Calculate average runtime of movies released in each country.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/mongodb-exercises/mongodb-movies-collection-exercise-21.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics