w3resource

Find movies with nominations from 'movies' collection


Find all movies with title, languages, released, directors, writers, awards, year, genres, runtime, cast, countries from the 'movies' collection in MongoDB that have at least one nomination.

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({
  "awards.nominations": { $gt: 0 }
}, {
  "title": 1,
  "languages": 1,
  "released": 1,
  "directors": 1,
  "writers": 1,
  "awards": 1,
  "year": 1,
  "genres": 1,
  "runtime": 1,
  "cast": 1,
  "countries": 1
})

Output:

  {
    _id: ObjectId("573a1390f29313caabcd5967"),
genres: [ 'Action', 'Adventure', 'Crime' ],
runtime: 399,
cast: [ 'Musidora', 'èdouardMathè', 'Marcel Lèvesque', 'Jean Aymè' ],
title: 'Les vampires',
languages: [ 'French' ],
released: ISODate("1916-11-23T00:00:00.000Z"),
directors: [ 'Louis Feuillade' ],
writers: [ 'Louis Feuillade' ],
awards: { wins: 0, nominations: 1, text: '1 nomination.' },
year: 1915,
countries: [ 'France' ]
  },
  {
    _id: ObjectId("573a1391f29313caabcd6d40"),
genres: [ 'Comedy', 'Short' ],
runtime: 26,
cast: [ 'Harold Lloyd', 'Roy Brooks', 'Mildred Davis', 'Wallace Howe' ],
title: 'High and Dizzy',
languages: [ 'English' ],
released: ISODate("1920-07-11T00:00:00.000Z"),
directors: [ 'Hal Roach' ],
writers: [ 'Frank Terry (story)', 'H.M. Walker (titles)' ],
awards: { wins: 0, nominations: 1, text: '1 nomination.' },
year: 1920,
countries: [ 'USA' ]
  },
  {
    _id: ObjectId("573a1391f29313caabcd68d0"),
genres: [ 'Comedy', 'Short', 'Action' ],
runtime: 22,
cast: [
      'Harold Lloyd',
      'Mildred Davis',
      "'Snub' Pollard",
      'Peggy Cartwright'
    ],
title: 'From Hand to Mouth',
languages: [ 'English' ],
released: ISODate("1919-12-28T00:00:00.000Z"),
directors: [ 'Alfred J. Goulding', 'Hal Roach' ],
writers: [ 'H.M. Walker (titles)' ],
awards: { wins: 0, nominations: 1, text: '1 nomination.' },
year: 1919,
countries: [ 'USA' ]
  },
....

Explanation:

The said query in MongoDB returns movies with their title, languages, released date, directors, writers, awards, year, genres, runtime, cast, and countries fields from the 'movies' collection in MongoDB that have at least one nomination.

In the find() method the $gt filters movies that have at least one nomination and the nominations field are within the awards subdocument. The projection includes the title, languages, released date, directors, writers, awards, year, genres, runtime, cast, and countries fields from the documents of the collection 'movies'.

Improve this sample solution and post your code through Disqus.

Previous: Retrieve movies with awards from 'movies' collection.
Next: Find Movies with "Charles Kayser" in the cast from 'movies' collection.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.