Counting Documents, Listing Values

Some questions do not need documents back. "How many movies are there?" only needs a number. countDocuments() gives you that number:

db.movies.countDocuments()
db.movies.countDocuments({ director: "Christopher Nolan" })

Output:

8
3

With no filter, it counts every document in the collection. With a filter, it counts only the documents that match. It returns just the number, which is much less data than the documents themselves.

"Which directors are in the collection?" needs a list of values. distinct() takes a field name and returns each value once:

db.movies.distinct("director")
db.movies.distinct("genres")

Output:

[ 'Bong Joon-ho', 'Christopher Nolan', 'Damien Chazelle' ]
[
  'drama',  'history',
  'music',  'romance',
  'sci-fi', 'thriller',
  'war'
]

Look at the second result. genres is an array, and distinct looked inside every array. It returned each genre once, even though most genres appear in several movies.

In the exercise, count the highly rated movies, then list the years.