w3resource

Top 10 directors by movie count


Find the top 10 directors with the most movies from the 'movies' collection in MongoDB.

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.aggregate([
  {
    $group: {
      _id: "$directors",
movieCount: { $sum: 1 }
    }
  },
  {
    $sort: { movieCount: -1 }
  },
  {
    $limit: 10
  }
])

Output:

{ _id: null, movieCount: 265 },
{ _id: [ 'Woody Allen' ], movieCount: 39 },
{ _id: [ 'Takashi Miike' ], movieCount: 33 },
{ _id: [ 'Alfred Hitchcock' ], movieCount: 31 },
{ _id: [ 'Werner Herzog' ], movieCount: 31 },
{ _id: [ 'John Huston' ], movieCount: 30 },
{ _id: [ 'Martin Scorsese' ], movieCount: 29 },
{ _id: [ 'John Ford' ], movieCount: 29 },
{ _id: [ 'Sidney Lumet' ], movieCount: 29 },
{ _id: [ 'Steven Spielberg' ], movieCount: 28 }

Explanation:

The said query in MongoDB retrieve the top 10 directors with the highest movie counts from the 'movies' collection in MongoDB.

The $group operator groups the movies by the "directors" field.

The $sum operator calculates the total number of movies per director.

The $sort operator sorts the grouped data based on the movieCount field in descending order (-1). That means the directors with the highest movie counts will appear at the beginning of the result set.

The $limit operator limits the output to only include the top 10 directors with the highest movie counts.

Improve this sample solution and post your code through Disqus.

Previous: Find movies with highest average rating by year.
Next: Calculate average movie ratings by rating category.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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://w3resource.com/mongodb-exercises/mongodb-movies-collection-exercise-25.php