w3resource

Movies with Viewer Rating between 3 and 4 on Tomatoes


Find all movies with title, languages, released, directors, viewer, writers, countries from the 'movies' collection in MongoDB that have a viewer rating of at least 3 and less than 4 on Tomatoes.

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(
{ 'tomatoes.viewer.rating': { $gte: 3, $lt: 4 } },
{ title: 1, languages: 1, released: 1, directors: 1, 'tomatoes.viewer': 1, writers: 1, countries: 1 }
)

Output:

  {
    _id: ObjectId("573a1391f29313caabcd6ea2"),
title: 'The Saphead',
countries: [ 'USA' ],
released: ISODate("1920-09-01T00:00:00.000Z"),
directors: [ 'Herbert Blachè', 'Winchell Smith' ],
writers: [
      'Bronson Howard (original play "The Henrietta")',
      'Victor Mapes (play)',
      'June Mathis (scenario)',
      'Winchell Smith (play)'
    ],
tomatoes: { viewer: { rating: 3.3, numReviews: 435, meter: 49 } }
  },
  {
    _id: ObjectId("573a1390f29313caabcd50e5"),
title: 'Gertie the Dinosaur',
languages: [ 'English' ],
released: ISODate("1914-09-15T00:00:00.000Z"),
directors: [ 'Winsor McCay' ],
writers: [ 'Winsor McCay' ],
countries: [ 'USA' ],
tomatoes: { viewer: { rating: 3.7, numReviews: 29 } }
  },
  {
    _id: ObjectId("573a1390f29313caabcd548c"),
title: 'The Birth of a Nation',
countries: [ 'USA' ],
released: ISODate("1915-03-03T00:00:00.000Z"),
directors: [ 'D.W. Griffith' ],
writers: [
      'Thomas Dixon Jr. (adapted from his novel: "The Clansman: An Historical Romance of the Ku Klux Klan")',
      'Thomas Dixon Jr. (play)',
      'Thomas Dixon Jr. (novel)',
      'D.W. Griffith',
      'Frank E. Woods'
    ],
tomatoes: { viewer: { rating: 3.2, numReviews: 4358, meter: 57 } }
  }

Explanation:

The said query in MongoDB returns movies with the title, languages, released, directors, viewer, writers, and countries fields from the 'movies' collection in MongoDB that have a viewer rating of at least 3 and less than 4 on Tomatoes.

In the find() method the $gte and the $lt operator checks for viewer ratings in the 'tomatoes' field greater than or equal to 3 and less than 4.

The projection object sets 1 to the fields title, languages, released, directors, tomatoes.viewer, writers, and countries to include them in the result set.

Improve this sample solution and post your code through Disqus.

Previous: Movies with 'scene' in the title.
Next: Retrieve Movies Released Before 1900.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.