$in: Match Any Value in a List

Sometimes a field can have one of several values. "Made in 2014 or 2019" is an example. $in checks a field against a list of values:

db.movies.find(
  { year: { $in: [2014, 2019] } },
  { _id: 0, title: 1, year: 1 }
)

Output:

[
  { title: 'Interstellar', year: 2014 },
  { title: 'Parasite', year: 2019 },
  { title: 'Whiplash', year: 2014 }
]

The list goes in square brackets. A document matches if its field is equal to any value in the list. The opposite is $nin (not in). It matches documents whose field is not equal to any value in the list:

db.movies.find(
  { year: { $nin: [2014, 2017] } },
  { _id: 0, title: 1, year: 1 }
)

Output:

[
  { title: 'Inception', year: 2010 },
  { title: 'Parasite', year: 2019 },
  { title: 'La La Land', year: 2016 },
  { title: 'First Man', year: 2018 }
]

When you want "this value or that value" for one field, $in is the shortest way to write it. You will also use $in with arrays in a later lesson.

In the exercise, find every movie by Bong Joon-ho or Damien Chazelle with one $in.