w3resource

Calculate average runtime of movies released in each country


Find all movies from the 'movies' collection in MongoDB with the average runtime of movies released in each country.

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([
  {
    $match: { type: 'movie' }
  },
  {
    $group: {
      _id: '$countries',
averageRuntime: { $avg: '$runtime' }
    }
  }
])

Output:

{ _id: [ 'Taiwan', 'USA', 'Germany' ], averageRuntime: 85 },
{ _id: [ 'UK', 'Spain', 'France' ], averageRuntime: 120 },
{ _id: [ 'Germany', 'Japan', 'UK' ], averageRuntime: 90 },
  {
    _id: [ 'UK', 'France', 'Germany', 'Switzerland', 'Luxembourg' ],
averageRuntime: 100
  },
  {
    _id: [ 'New Zealand', 'Germany' ],
averageRuntime: 95.33333333333333
  },
{ _id: [ 'Netherlands', 'Belgium', 'Germany' ], averageRuntime: 140 },
{ _id: [ 'France', 'Morocco' ], averageRuntime: 103.2 },
{ _id: [ 'France', 'Russia', 'USA' ], averageRuntime: 96 },
{ _id: [ 'Finland', 'Sweden', 'Germany' ], averageRuntime: 128 },
  {
    _id: [ 'Croatia', 'Bosnia and Herzegovina' ],
averageRuntime: 84.5
  },
  {
    _id: [ 'Slovenia', 'Ireland', 'Finland', 'Sweden' ],
averageRuntime: 77
  },
  {
    _id: [ 'France', 'Norway', 'Belgium', 'Poland' ],
averageRuntime: 91
  },
{ _id: [ 'Israel', 'Bulgaria' ], averageRuntime: 90 },
  {
    _id: [ 'Slovakia', 'Czech Republic', 'UK', 'USA', 'Israel', 'Cambodia' ],
averageRuntime: 96
  },
{ _id: [ 'Canada', 'South Africa' ], averageRuntime: 101.5 },
{ _id: [ 'China', 'South Korea', 'Singapore' ], averageRuntime: 110 },
{ _id: [ 'Finland', 'France' ], averageRuntime: 80 },
{ _id: [ 'Italy', 'Germany', 'Switzerland' ], averageRuntime: 88 },
{ _id: [ 'France', 'Russia', 'Ukraine' ], averageRuntime: 87 },
{ _id: [ 'USA', 'UK', 'Australia' ], averageRuntime: 115.4 }

.....

Explanation:

The said query in MongoDB returns movies from the 'movies' collection in MongoDB that have the average runtime of movies based on each country.

The $match stage filters the documents where the "type" field is equal to "movie" and the $group stage groups the documents by the "countries" field.

Inside the $group stage, the $avg aggregation operator calculates the average runtime of movies in each group. The result of the average runtime calculation is stored in the field "averageRuntime".

The result of this aggregation pipeline is a list of groups and each group represents a unique combination of countries. For each group the query includes the countries, and the "averageRuntime".

Improve this sample solution and post your code through Disqus.

Previous: Retrieve top 5 movies with highest imdb ratings.
Next: Retrieve movies with awards from 'movies' collection.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.