Movies with runtime between 60 and 90 minutes
Find all movies with title, languages, released, runtime, directors, writers, countries from the 'movies' collection in MongoDB that have a runtime between 60 and 90 minutes.
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(
{ runtime: { $gte: 60, $lte: 90 } },
{ title: 1, languages: 1, released: 1, runtime: 1, directors: 1, writers: 1, countries: 1 }
);
Output:
{ _id: ObjectId("573a1391f29313caabcd715c"), runtime: 68, title: 'The Kid', languages: [ 'English' ], released: ISODate("1921-02-06T00:00:00.000Z"), directors: [ 'Charles Chaplin' ], writers: [ 'Charles Chaplin' ], countries: [ 'USA' ] }, { _id: ObjectId("573a1391f29313caabcd7586"), runtime: 79, title: 'Nanook of the North', countries: [ 'USA', 'France' ], released: ISODate("1922-06-11T00:00:00.000Z"), directors: [ 'Robert J. Flaherty' ] }, { _id: ObjectId("573a1391f29313caabcd75b5"), runtime: 81, title: 'Nosferatu', languages: [ 'German' ], released: ISODate("1929-06-03T00:00:00.000Z"), directors: [ 'F.W. Murnau' ], writers: [ 'Henrik Galeen (screen play)', 'Bram Stoker (based on the novel: "Dracula")' ], countries: [ 'Germany' ] }, { _id: ObjectId("573a1391f29313caabcd79e4"), runtime: 63, title: 'Three Ages', countries: [ 'USA' ], released: ISODate("1923-09-24T00:00:00.000Z"), directors: [ 'Edward F. Cline', 'Buster Keaton' ], writers: [ 'Clyde Bruckman (story)', 'Joseph A. Mitchell (story)', 'Jean C. Havez (story)' ] } ......
Explanation:
The said query in MongoDB returns movies with the 'title', 'languages', 'released', 'runtime', 'directors', 'writers', and 'countries' fields from the 'movies' collection in MongoDB that meet the runtime between 60 and 90 minutes.
The $gte and $lte operators in the find() method matches movies with a runtime between 60 and 90 minutes.
In the second parameter the fields with value '1' indicates the inclusion of the 'title', 'languages', 'released', 'runtime', 'directors', 'writers', and 'countries' fields to the output.
Improve this sample solution and post your code through Disqus.
Previous: Movies with "metal" in the full plot.
Next: Retrieve top 5 movies with highest imdb ratings.
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-20.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics