$or, and How to Write AND
$in works when you test one field. When either of two conditions on different fields is enough, use $or. It takes an array of filters:
db.movies.find(
{ $or: [
{ rating: { $gte: 8.7 } },
{ year: { $lt: 2014 } }
] },
{ _id: 0, title: 1, year: 1, rating: 1 }
)
Output:
[
{ title: 'Inception', year: 2010, rating: 8.8 },
{ title: 'Interstellar', year: 2014, rating: 8.7 }
]
Each item in the array is a complete filter. A document matches if at least one of them matches. Inception matches both filters (rating 8.8 and year 2010), but it still appears only once.
What about "and"? You already have it. When a filter has more than one field, a document must match all of them:
db.movies.find(
{ director: "Christopher Nolan", year: { $gt: 2013 } },
{ _id: 0, title: 1, year: 1 }
)
Output:
[
{ title: 'Interstellar', year: 2014 },
{ title: 'Dunkirk', year: 2017 }
]
This means "directed by Nolan and made after 2013". There is also an $and operator, but you rarely need it. A comma between conditions already means "and".
In the exercise, use $or to find movies rated 8.7 or higher, or made before 2014.