Retrieve award-winning movies sorted by year

Write a query in MongoDB to find the oldest movie with an award win from the ‘movies’ collection.

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")


db.movies.find({ "awards.wins": { $gt: 0 } }).sort({ year: 1 }).limit(1)


    _id: ObjectId("573a13a3f29313caabd0d5a4"),
plot: 'An athlete swings Indian clubs.',
genres: [ 'Documentary', 'Short' ],
runtime: 1,
title: 'Newark Athlete',
num_mflix_comments: 3,
countries: [ 'USA' ],
fullplot: 'A young man stands before the camera holding a club in each hand, horizontal to the ground. He raises the heads of the two clubs in unison, by rotating the clubs without lifting his arms. The film then shows the same footage over again, at different speeds.',
languages: [ 'English' ],
directors: [ 'William K.L. Dickson' ],
rated: 'NOT RATED',
awards: { wins: 1, nominations: 0, text: '1 win.' },
lastupdated: '2015-08-03 00:57:26.680000000',
year: 1891,
imdb: { rating: 4.9, votes: 827, id: 241763 },
type: 'movie',
tomatoes: { lastUpdated: ISODate("2012-09-30T00:00:00.000Z") }


The said query in MongoDB that retrieves the movie with the earliest year of release that has won at least one award from the 'movies' collection in MongoDB.

The find() method searches for documents where the "awards.wins" field has a value greater than zero.

The sort() method sorts the documents based on the field "year" in ascending order indicated by the value of 1.

The limit() method limits to return only one that is the earliest movie, based on the "year" field.

Improve this sample solution and post your code through Disqus.

Previous: Calculate average movie ratings by rating category.
Next: MongoDB query to retrieve top-rated movie.

What is the difficulty level of this exercise?

Follow us on Facebook and Twitter for latest update.